Proving Equivalences HDU
来源:互联网 发布:数据价值网 邹志飞 编辑:程序博客网 时间:2024/06/17 12:34
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:
- A is invertible.
- Ax = b has exactly one solution for every n × 1 matrix b.
- Ax = b is consistent for every n × 1 matrix b.
- 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
这个问题核心是对于一个有向图,应该最少加多少条边,才能变成强连通图,这个有一类问题,首先把所有的连通分量都找出来,那么每一个强连通分量的每一个点可以说是等价的,我们把它看作一个点,那么这些点又会构成一个图,准确的说是有向无环图,所有我们只要看这个图的出度为0的点有多少,如度为0的店有多少,然后其最大值即为所需要加的边
#include<cstdio>#include<algorithm>#include<cmath>#include<iostream>#include<stack>#include<vector>#include<cstring>#define N 20005using namespace std;int index;int DFN[N];int low[N];bool inS[N];int be[N];int inD[N];int outD[N];stack<int> s;vector<int>graph[N];int n,m;int countt;void init(int n){ while(!s.empty()) s.pop(); memset(DFN,0,sizeof(DFN)); memset(inS,false,sizeof(inS)); memset(inD,0,sizeof(inD)); memset(outD,0,sizeof(outD)); for(int i=1;i<=n;i++) graph[i].clear(); index=1; countt=0;}void tarjan(int x){ DFN[x]=low[x]=index++; inS[x]=true; s.push(x); for(int i=0;i<graph[x].size();i++) { int v=graph[x][i]; if(!DFN[v]) { tarjan(v); //if(sign)return; low[x]=min(low[x],low[v]); } else if(inS[v]) low[x]=min(low[x],DFN[v]); } if(DFN[x]==low[x]) { countt++; while(x!=s.top()) { inS[s.top()]=false; be[s.top()]=countt;//给每个点做一个编号,说明这个点属于哪个连通分量 s.pop(); } inS[s.top()]=false; be[s.top()]=countt; s.pop(); }}//这些都是在tarjan算法的基础上做了一些修饰int solve(){ if(countt==1) return 0; int v; for(int i=1;i<=n;i++) { for(int j=0;j<graph[i].size();j++) { v=graph[i][j]; if(be[i]!=be[v]) { outD[be[i]]++; inD[be[v]]++;//统计每个连通分量的入读和出度 } } } int out=0,in=0; for(int i=1;i<=countt;i++) { out+=(outD[i]==0); in+=(inD[i]==0); } return max(out,in);//返回最大即可}int main(){ int a,b; int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); init(n); while(m--) { scanf("%d%d",&a,&b); graph[a].push_back(b); } for(int i=1;i<=n;i++) if(!DFN[i]) tarjan(i); //cout<<countt<<endl; printf("%d\n",solve()); } return 0;}
- 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 2767 Proving Equivalences
- HDU 2767 Proving Equivalences
- hdu 2767 Proving Equivalences
- HDU 2767 Proving Equivalences
- hdu 2767 Proving Equivalences
- Proving Equivalences HDU
- Proving Equivalences HDU
- Proving Equivalences HDU
- Proving Equivalences
- Proving Equivalences
- hdu 2767 Proving Equivalences (tarjan + 缩点)
- hdu 2767 Proving Equivalences(强连通分量)
- 特征分解条件
- 了解
- 1004. Counting Leaves (30)
- 2017.7.31 SN号与tls测试
- Python 正则表达式(模式匹配)
- Proving Equivalences HDU
- Pycharm连接Github,下载上传管理
- 欢迎使用CSDN-markdown编辑器
- freemark常用
- java中charAt()与toCharArray(),indexOf的综合使用
- windows下使用PHP实现定时执行脚本
- git add详解(总记不住啥时候用add的哪个参数、囧)
- HashMap.comparableClassFor(Object x)方法解读:类型/泛型相关方法
- 通过js使用动画曲线