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

NWERC 2008

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 }
时间: 2024-08-04 12:17:47

HDU2767 Proving Equivalences的相关文章

hdu2767 Proving Equivalences --- 强连通

给一个图,问至少加入?多少条有向边能够使图变成强连通的. 原图是有环的,缩点建图,在该DAG图上我们能够发现,要使该图变成强连通图必须连成环 而加入?最少的边连成环,就是把图上入度为0和出度为0的点连上,那么其它的点就都能够互相到达了 所以答案就是max(入度为0的点,出度为0的点) #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <

hdu2767 Proving Equivalences有向图的强连通_Tarjan缩点

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4343    Accepted Submission(s): 1541 Problem Description Consider the following exercise, found in a generic linear algebra textbook. Let A be an

hdu2767 Proving Equivalences,有向图强联通,Kosaraju算法

点击打开链接 有向图强联通,Kosaraju算法 缩点后分别入度和出度为0的点的个数 answer = max(a, b); scc_cnt = 1; answer = 0 #include<cstdio> #include<algorithm> #include<vector> #include<cstring> #include<stack> using namespace std; const int maxn = 20000 + 10;

hdu 2767 Proving Equivalences(强连通入门题)

1 /************************************************* 2 Proving Equivalences(hdu 2767) 3 强连通入门题 4 给个有向图,求至少加多少条边使得图是所有点都是强连通的 5 由a->b->c->a易知n个点至少要n条边,每个出度和入度都要大 6 于1.先求所有所有强连通分量,把每个强连通分量看成一个点 7 在找每个点的出度和入度,最后还差的出度和入度的最大值就是 8 答案. 9 10 ************

Proving Equivalences(加多少边使其强联通)

Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4384    Accepted Submission(s): 1556 Problem Description Consider the following exercise, found in a generic linear algebra t

UVA 12167 - Proving Equivalences(强连通分量+缩点)

UVA 12167 - Proving Equivalences 题目链接 题意:给定一些已经存在的等价性证明,要求全部等价,需要在多最少几次证明 思路:先求出强连通分量,然后进行缩点,在缩点后的图上统计入度和出度为0结点的最大值,就是需要加的边数,注意如果整个图已经是强连通,就直接是答案 代码: #include <cstdio> #include <cstring> #include <vector> #include <stack> #include

HDU 2767 Proving Equivalences(强联通缩点)

Proving Equivalences 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

HDU 2767 Proving Equivalences (强联通)

http://acm.hdu.edu.cn/showproblem.php?pid=2767 Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2926    Accepted Submission(s): 1100 Problem Description Consider the followi

HDU 2767 Proving Equivalences

 Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3676    Accepted Submission(s): 1352 Problem Description Consider the following exercise, found in a generic linear algeb