Proving Equivalences

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9208    Accepted Submission(s): 3257

Problem Description
Consider the following exercise, found in a generic linear algebra textbook.

Let A be an n × n matrix. Prove that the following statements are equivalent:

1. A is invertible.
2. Ax = b has exactly one solution for every n × 1 matrix b.
3. Ax = b is consistent for every n × 1 matrix b.
4. Ax = 0 has only the trivial solution x = 0.

The typical way to solve such an exercise is to show a series of implications. For instance, one can proceed by showing that (a) implies (b), that (b) implies (c), that (c) implies (d), and finally that (d) implies (a). These four implications show that the four statements are equivalent.

Another way would be to show that (a) is equivalent to (b) (by proving that (a) implies (b) and that (b) implies (a)), that (b) is equivalent to (c), and that (c) is equivalent to (d). However, this way requires proving six implications, which is clearly a lot more work than just proving four implications!

I have been given some similar tasks, and have already started proving some implications. Now I wonder, how many more implications do I have to prove? Can you help me determine this?

 
Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:

* One line containing two integers n (1 ≤ n ≤ 20000) and m (0 ≤ m ≤ 50000): the number of statements and the number of implications that have already been proved.
* m lines with two integers s1 and s2 (1 ≤ s1, s2 ≤ n and s1 ≠ s2) each, indicating that it has been proved that statement s1 implies statement s2.

 
Output
Per testcase:

* One line with the minimum number of additional implications that need to be proved in order to prove that all statements are equivalent.

 
Sample Input
2
4 0
3 2
1 2
1 3
 
Sample Output
4
2
 
Source
 
 
代码:
 #include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
int head[N],dfn[N],low[N],belong[N],stak[N],instack[N];
int in[N],out[N];
int incnt,outcnt;
int cnt,indexx,top,ans;
struct node{
int u,v,next;
}edge[N*]; void add(int u,int v)
{
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
} void Init()
{
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(instack,,sizeof(instack));
cnt=indexx=top=ans=;
memset(in,,sizeof(in));
memset(out,,sizeof(out));
incnt=outcnt=;
} void tarjan(int u)
{
dfn[u]=low[u]=++indexx;
stak[++top]=u;
instack[u]=;
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(!dfn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){
ans++;
while(){
int v=stak[top--];
instack[v]=;
belong[v]=ans;
if(u==v)
break;
}
}
} int main()
{
int T,n,m;
int u,v;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
Init();
while(m--){
scanf("%d%d",&u,&v);
add(u,v);
}
for(int i=; i<=n; i++){
if(!dfn[i])
tarjan(i);
}
if(ans==){
printf("0\n");
continue;
}
for(int i=; i<=n; i++){
for(int j=head[i]; j!=-; j=edge[j].next){
int v=edge[j].v;
if(belong[v]!=belong[i]){
in[belong[v]]++;
out[belong[i]]++;
}
}
}
for(int i=; i<=ans; i++){
if(!in[i])
incnt++;
if(!out[i])
outcnt++;
}
printf("%d\n",max(incnt,outcnt));
}
return ;
}

最新文章

  1. CSS中对图片(background)的一些设置心得总结
  2. wordpress-4.4.1 数据库表结构解析
  3. kafka常用命令
  4. flex+java+blazeds 多通道好文
  5. python入门教程链接
  6. [Tool] 常用开发工具注册码(持续更新)
  7. make_pair
  8. SQL 将一列多行数据合并为一行 FOR XML PATH
  9. redis 2.4异常
  10. Spring顶级项目以及Spring cloud组件
  11. 使用Razor来进行页面布局
  12. POJ - 1182 食物链 并查集经典
  13. 杭电ACM2019--数列有序!
  14. leetcode 169. Majority Element 、229. Majority Element II
  15. shell编程其实真的很简单(一)
  16. ipconfig命令查ip的时候给别人看有危险吗
  17. linux-安装jdk以及tomcat
  18. 关于ArrayList中的iterator返回的事迭代器实例问题。
  19. dell 远程管理卡的使用racadm
  20. POJ 2485

热门文章

  1. A1065 A+B and C (64bit) (20)(20 分)
  2. 笔记-python-反射
  3. TCP/IP网络编程之套接字与标准I/O
  4. 相当牛X的java版星际游戏
  5. HTML textarea 无法修改 value 的问题
  6. sqlserver执行时间和自行效率
  7. Unity属性——AddComponentMenu
  8. 【bzoj2836】魔法树 树链剖分+线段树
  9. POJ2152 Fire 【树形dp】
  10. 七牛云的cdn配置