HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)

Posted Kaiser

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)相关的知识,希望对你有一定的参考价值。

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?

InputOn 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.OutputPer 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

题意: 
  给定一张有向图,问最少添加几条边使得有向图成为一个强连通图。

题解:
  缩完点的图是一个DAG,变成强联通就是,一个点至少一个出度一个入度
  所以只需要输出缩完点后的图入度和出度最大值既可。
   这个真的很好想,自己瞎比比搞了半天,浪费了许多时间。
  真的菜。
  

  想到后怒删代码,修改就过了。

 1 #include<cstring>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstdio>
 6 #define N 20007
 7 #define M 50007
 8 using namespace std;
 9 
10 int n,m,tim,sc,totalin,totalout;
11 int top,dfn[N],low[N],stack[N],ins[N],bel[N],chu[N],ru[N],boo[N];
12 int cnt,head[N],Next[M],rea[M];
13 struct Node
14 {
15     int ru,chu;
16     void init()
17     {
18         ru=chu=0;
19     }
20 }zhi[N];
21 
22 void add(int u,int v)
23 {
24     Next[++cnt]=head[u];
25     head[u]=cnt;
26     rea[cnt]=v;
27 }
28 void Tarjan(int u)
29 {
30     dfn[u]=low[u]=++tim;
31     stack[++top]=u,ins[u]=true;
32     for (int i=head[u];i!=-1;i=Next[i])
33     {
34         int v=rea[i];
35         if (!dfn[v])
36         {
37             Tarjan(v);
38             low[u]=min(low[u],low[v]);
39         }
40         else if (ins[v]) low[u]=min(low[u],dfn[v]);
41     }
42     if (low[u]==dfn[u])
43     {
44         sc++;int x=-1;
45         while(x!=u)
46         {
47             x=stack[top--];
48             ins[x]=0;
49             bel[x]=sc;
50         }
51     }
52 }
53 void rebuild()
54 {
55     for (int u=1;u<=n;u++)
56     {
57         for (int i=head[u];i!=-1;i=Next[i])
58         {
59             int v=rea[i];
60             if (bel[v]!=bel[u])
61             {
62                 chu[bel[u]]++;
63                 ru[bel[v]]++;
64             }
65         }
66     }
67     for (int i=1;i<=sc;i++)
68     {
69         if (!chu[i]) totalout++;
70         if (!ru[i]) totalin++;
71     }
72 }
73 int main()
74 {
75     int T;scanf("%d",&T);
76     while (T--)
77     {
78         cnt=sc=0,top=0,totalin=totalout=0;
79         memset(head,-1,sizeof(head));
80         memset(dfn,0,sizeof(dfn));
81         memset(low,0,sizeof(low));
82         memset(boo,0,sizeof(boo));
83         memset(chu,0,sizeof(chu));
84         memset(ru,0,sizeof(ru));
85         scanf("%d%d",&n,&m);
86         for (int i=1,x,y;i<=m;i++)
87         {
88             scanf("%d%d",&x,&y);
89             add(x,y);
90         }
91         for (int i=1;i<=n;i++)
92             if (!dfn[i]) Tarjan(i);
93         rebuild();
94         int ans=max(totalout,totalin);
95         if (ans==1) ans=0;
96         printf("%d\\n",ans);    
97     }
98 }

 

以上是关于HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 2767 Proving Equivalences (Tarjan)

HDU2767Proving Equivalences[tarjan 缩点]

HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)

Proving Equivalences (hdu 2767 强联通缩点)

HDU 2767-Proving Equivalences(强联通+缩点)

HDU 2767:Proving Equivalences(强连通)