博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【HDOJ2767】【Tarjan缩点】
阅读量:5019 次
发布时间:2019-06-12

本文共 4283 字,大约阅读时间需要 14 分钟。

Proving Equivalences

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 8605    Accepted Submission(s): 3063

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
题目大意:给一个有向图,问至少加几条边能使图成为强连通图。
题目分析:先用Tarjan缩点,然后统计每个强连通分量的入度与出度,统计入度为0的强连通分量的个数以及出度为0的强连通分量的个数,求两个数的最大值即可
【PS:注意强连通分量为1个时应进行特判,因为他的入度和出度都为0,但是不用加边就已经是强连通图了】
1 //Wannafly挑战赛14 C https://www.nowcoder.com/acm/contest/81/C  2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 using namespace std; 9 const int maxn=100005; 10 struct edge{ 11 int from; 12 int to; 13 int next; 14 }EDGE[maxn]; 15 vector
vc[maxn]; 16 int head[maxn],dfn[maxn],vis[maxn],low[maxn],col[maxn],in[maxn],out[maxn],en[maxn],stk[maxn];//各个变量的意义可参照上篇博客 17 int edge_cnt=1,tot1=0,tot2=0,scc_cnt=0,tot0=0; 18 void add(int x,int y) 19 { 20 EDGE[edge_cnt].from=x; 21 EDGE[edge_cnt].to=y; 22 EDGE[edge_cnt].next=head[x]; 23 head[x]=edge_cnt++; 24 } 25 void Tarjan(int u) 26 { 27 low[u]=dfn[u]=++tot1;//注意tot1的初值必须是1【因为dfn必须为正数】,所以这里使用++tot1而不用tot1++; 28 vis[u]=1; 29 stk[++tot2]=u; 30 for(int i = head[u]; i != -1 ; i = EDGE[i].next) 31 { 32 if(!dfn[EDGE[i].to]){ 33 Tarjan(EDGE[i].to); 34 low[u]=min(low[u],low[EDGE[i].to]); 35 } 36 else if(vis[EDGE[i].to]){ 37 low[u]=min(low[u],low[EDGE[i].to]); 38 } 39 } 40 if(low[u]==dfn[u]){ 41 int xx; 42 scc_cnt++; 43 do{ 44 xx=stk[tot2--]; 45 vc[scc_cnt].push_back(xx); 46 col[xx]=scc_cnt; 47 vis[xx]=0; 48 }while(xx!=u); 49 } 50 } 51 int main() 52 { 53 int t; 54 scanf("%d",&t); 55 while(t--) 56 { 57 edge_cnt=0,tot1=0,tot2=0,scc_cnt=0,tot0=0; 58 scc_cnt=0; 59 int n,m; 60 scanf("%d%d",&n,&m); 61 memset(head,-1,sizeof(head)); 62 memset(stk,0,sizeof(stk)); 63 memset(in,0,sizeof(in)); 64 memset(out,0,sizeof(out)); 65 memset(dfn,0,sizeof(dfn)); 66 memset(low,0,sizeof(low)); 67 memset(col,0,sizeof(col)); 68 while(m--) 69 { 70 int a,b; 71 scanf("%d%d",&a,&b); 72 add(a,b); 73 } 74 for(int i = 1 ; i <= n; i++) 75 { 76 if(!dfn[i])Tarjan(i); 77 } 78 for(int i = 0 ; i < edge_cnt ; i++) 79 { 80 if(col[EDGE[i].from]!=col[EDGE[i].to]) 81 { 82 in[col[EDGE[i].to]]++;//缩点 83 out[col[EDGE[i].from]]++; 84 } 85 } 86 int sum1=0,sum2=0; 87 for(int i = 1 ; i <= scc_cnt ; i++) 88 { 89 if(!in[i]) 90 sum1++; 91 if(!out[i]) 92 sum2++; 93 } 94 int mmax=max(sum1,sum2); 95 if(scc_cnt!=1) 96 cout << mmax << endl; 97 else 98 cout << "0" <

 

转载于:https://www.cnblogs.com/MekakuCityActor/p/9021775.html

你可能感兴趣的文章
atom 调用g++编译cpp文件
查看>>
H3C HDLC协议特点
查看>>
iptables 网址转译 (Network address translation,NAT)
查看>>
ios __block typeof 编译错误解决
查看>>
android 插件形式运行未安装apk
查看>>
ios开发之 manage the concurrency with NSOperation
查看>>
Android权限 uses-permission
查看>>
NSEnumerator用法小结
查看>>
vim如何配置go语言环境
查看>>
机器学习好网站
查看>>
python 中的 sys , os 模块用法总结
查看>>
解题:国家集训队 Middle
查看>>
响应者链
查看>>
指针从函数内部带回返回值
查看>>
在使用webView播放flash或视频文件时无法关闭声音的问题
查看>>
redhat 7 源码安装 mysql5.5.49
查看>>
CCP浅谈
查看>>
NAT虚拟网络配置
查看>>
c#部分---需要实例化的内容;
查看>>
销售类
查看>>