Proving Equivalences HDU - 2767

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

题解:对每个点进行编号,属于同一个强连通分量的顶点编号相同。然后枚举每条边,如果这条边的两个端点不在同一个强连通分量,则两个端点

所在的强连通分量的入度,出度分别加1。那么为啥最后取最大值?因为整个图是强连通的,所以不存在入度为0的点和出度为0的点,所以连的边

必是在出度或入度为0的点之间。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<vector>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 const int maxn=2e4+4;
 9 
10 int n,m;
11 int cmp[maxn],in[maxn],out[maxn];
12 bool use[maxn];
13 
14 vector<int> vs;
15 vector<int> G1[maxn];
16 vector<int> G2[maxn];
17 
18 void Inite(){
19     vs.clear();
20     for(int i=1;i<=n;i++){
21         G1[i].clear();
22         G2[i].clear();
23     }
24 }
25 
26 void addedge(int u,int v){
27     G1[u].push_back(v);
28     G2[v].push_back(u);
29 }
30 
31 void DFS1(int v){
32     use[v]=true;
33     for(int i=0;i<G1[v].size();i++) if(!use[G1[v][i]]) DFS1(G1[v][i]);
34     vs.push_back(v);
35 }
36 
37 void DFS2(int v,int k){
38     use[v]=true;
39     cmp[v]=k;
40     for(int i=0;i<G2[v].size();i++) if(!use[G2[v][i]]) DFS2(G2[v][i],k);
41 }
42 
43 int Scc(){
44     memset(use,0,sizeof(use));
45     for(int i=1;i<=n;i++) if(!use[i]) DFS1(i);
46     memset(use,0,sizeof(use));
47     
48     int k=0,b=vs.size()-1;
49     for(int i=b;i>=0;i--) if(!use[vs[i]]) DFS2(vs[i],k++);
50     return k;
51 }
52 
53 void Solve(int k){
54     
55     memset(in,0,sizeof(in));
56     memset(out,0,sizeof(out));
57     
58     for(int u=1;u<=n;u++){
59         for(int i=0;i<G1[u].size();i++){
60             int v=G1[u][i];
61             if(cmp[u]==cmp[v]) continue;       //并不用判断u,v所在的连通分量是否已经计算过,因为最后找的是入度或者出度为0的编号
62         
63             in[cmp[v]]++;
64             out[cmp[u]]++;
65         }
66     }
67     
68     int ans1=0,ans2=0;         
69     for(int u=0;u<k;u++){
70         //cout<<in[u]<<" "<<out[u]<<endl;
71         if(in[u]==0) ans1++;
72         if(out[u]==0) ans2++;
73     }
74     cout<<max(ans1,ans2)<<endl;          //需要仔细想一下,为什么答案是取max!
75 }
76 
77 int main()
78 {   int kase;
79     scanf("%d",&kase);
80     while(kase--){
81         Inite();
82         scanf("%d%d",&n,&m);
83         
84         if(n==1){ cout<<0<<endl; continue; } 
85         if(m==0){ cout<<n<<endl; continue; } 
86         
87         for(int i=0;i<m;i++){
88             int a,b;
89             scanf("%d%d",&a,&b);
90             addedge(a,b);
91         }
92         
93         int p=Scc();
94         if(p==1){ cout<<0<<endl; continue; }
95         Solve(p);
96     }
97     return 0;    
98 }

 

posted @ 2017-11-23 15:55  天之道,利而不害  阅读(178)  评论(0)    收藏  举报