hdu 2767 Proving Equivalences(至少加几条边让整个图变成强连通)
来源:互联网 发布:ubuntu 安装jdk1.8 编辑:程序博客网 时间:2024/06/10 18:09
Proving Equivalences
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2088 Accepted Submission(s): 796
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?
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.
* 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.
* 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
24 03 21 21 3
Sample Output
42题意:同hdu 3836是一样的, 问至少加几条边让整个图变成强连通。思路:这里用tarjan算法缩点。AC代码:#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <vector>#include <cmath>#include <cstdlib>#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)using namespace std;const int maxn=20005;const int maxm=50005;struct node{ int v,next;} edge[maxm];int head[maxn],low[maxn],dfn[maxn];int stack[maxn],scc[maxn],in[maxn],out[maxn];bool ins[maxn];int n,m,top,cnt,snum,num;void init(){ memset(head,-1,sizeof(head)); num=0;}void add(int u,int v){ edge[num].v=v; edge[num].next=head[u]; head[u]=num++;}void input(){ int a,b; scanf("%d%d",&n,&m); while(m--) { scanf("%d%d",&a,&b); add(a,b); }}void dfs(int u){ int x; dfn[u]=low[u]=++cnt; stack[top++]=u; ins[u]=true; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(!dfn[v]) { dfs(v); low[u]=min(low[u],low[v]); } else if(ins[v]) low[u]=min(low[u],dfn[v]); } if(low[u]==dfn[u]) { ++snum; do{ x=stack[--top]; ins[x]=false; scc[x]=snum; }while(x!=u); }}void tarjan(){ memset(dfn,0,sizeof(dfn)); memset(ins,false,sizeof(ins)); snum=cnt=top=0; for(int i=1;i<=n;i++) if(!dfn[i]) dfs(i);}void solve(){ memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); for(int u=1;u<=n;u++) for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(scc[u]!=scc[v]) { out[scc[u]]++; in[scc[v]]++; } } if(snum==1) { printf("0\n");return;} int ans1=0,ans2=0; for(int i=1;i<=snum;i++) { if(!in[i]) ans1++; if(!out[i]) ans2++; } printf("%d\n",max(ans1,ans2));}int main(){ int t; scanf("%d",&t); while(t--) { init(); input(); tarjan(); solve(); } return 0;}
- hdu 2767 Proving Equivalences(至少加几条边让整个图变成强连通)
- HDU 2767 Proving Equivalences (HDU 3836) 至少加几条边让整个图变成强连通+邻接表建图
- 2767 Proving Equivalences 至少加几条边让全部图变成强连通模板题
- HDU2767Proving Equivalences(强连通+缩点+ 至少加几条边让整个图变成强连通))
- hdu 2767 Proving Equivalences(强连通分量)
- hdu 2767 Proving Equivalences 强连通
- HDU 2767 Proving Equivalences 强连通
- HDU 2767 Proving Equivalences(强连通分量)
- HDU 2767:Proving Equivalences【强连通】
- HDU 2767 Proving Equivalences(强连通分量)
- HDU 2767Proving Equivalences 强连通分量
- HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)
- HDU 2767 Proving Equivalences(强连通分量)
- hdu 2767Proving Equivalences(强连通分量压缩 )
- HDU 2767 Proving Equivalences(强连通分量)
- HDU-2767 Proving Equivalences (强连通分量[Tarjan])
- hdu 2767 Proving Equivalences(强连通分量+缩点)
- [HDU 2767]Proving Equivalences(强连通分量)
- CollabNet Subversion的Windows安装及使用说明
- Oracle 11g r2 for linux(CentOS 6.4) 遇到的两个小问题
- C++经典错误之already defined in .obj解决方法之补充
- 表格的四种基本格式
- redhat 6 配置 yum 源的两种方法
- hdu 2767 Proving Equivalences(至少加几条边让整个图变成强连通)
- nyoj-143-第几是谁
- jsp导出excell,word
- IOS数据库操作SQLite3使用详解
- poj(1274)The Perfect Stall
- [转]ThoughtWorks(中国)程序员读书雷达
- CentOS快速安装最新版本的SaltStack
- POJ 1222 EXTENDED LIGHTS OUT 解题报告(高斯消元法)
- solaris tar 文件名比 100 长