HDU2767 Proving Equivalences

 

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


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
 

 

Recommend
lcy
 
理解一下题意,经过奇奇怪怪的转化以后,得出核心题意:给一个有向图,问最少加几条边可使其成为强连通图。
tarjan缩点以后,统计入度为0和出度为0的点个数,取最大值就是答案。
 
这题总感觉以前做过好多次?
 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<vector>
 5 #include<cstring>
 6 using namespace std;
 7 const int mxn=48000;
 8 int top,stack[mxn];
 9 bool inst[mxn];
10 int cnt,dnow;
11 int dfn[mxn],low[mxn];
12 int belone[mxn],in[mxn],out[mxn];
13 vector<int> e[mxn];
14 void clear(){
15     cnt=0;dnow=0;top=0;
16     memset(dfn,-1,sizeof(dfn));
17     memset(inst,false,sizeof(inst));
18     memset(in,0,sizeof in);
19     memset(out,0,sizeof out);
20     for(int i=1;i<mxn;i++) e[i].clear();
21 }
22 int n,m;
23 void tarjan(int s){
24     int v=0,i;
25     dfn[s]=++dnow;
26     low[s]=dfn[s];
27     inst[s]=true; 
28     stack[++top]=s;
29     int si=e[s].size();
30     for(i=0;i<si;i++){
31         v=e[s][i];
32         if(dfn[v]==-1){
33             tarjan(v);
34             low[s]=min(low[v],low[s]);
35         }
36         else if(inst[v]){
37             low[s]=min(dfn[v],low[s]);
38         }
39     }
40     if(dfn[s]==low[s]){
41         cnt++;
42         do{
43             v=stack[top--];
44             belone[v]=cnt;
45             inst[v]=false;
46         }while(s!=v);
47     }
48     return;
49 }
50 void calc(){
51     if(cnt==1){
52         printf("0\n");return;
53     }
54     int i,j;
55     for(i=1;i<=n;i++){
56         for(j=0;j<e[i].size();j++){
57             int v=e[i][j];
58             if(belone[i]!=belone[v]){
59                 in[belone[v]]++;
60                 out[belone[i]]++;
61             }
62         }
63     }
64     int idg=0,odg=0;
65     for(i=1;i<=cnt;i++){
66         if(!in[i])idg++;
67         if(!out[i])odg++;
68     }
69     printf("%d\n",max(idg,odg));
70     return;
71 }
72 int main(){
73     int T;
74     scanf("%d",&T);
75     while(T--){
76         scanf("%d%d",&n,&m);
77         if(!m){
78             if(n==1)printf("0\n");
79             else printf("%d\n",n);
80             continue;
81         }
82         clear();
83         int i,j;
84         int u,v;
85         for(i=1;i<=m;i++){
86             scanf("%d%d",&u,&v);
87             e[u].push_back(v);
88         }
89         for(i=1;i<=n;i++){
90             if(dfn[i]==-1)tarjan(i);
91         }
92         calc();
93     }
94     return 0;
95 }

 

posted @ 2016-08-06 18:43  SilverNebula  阅读(182)  评论(0编辑  收藏  举报
AmazingCounters.com