hdoj 2767 Proving Equivalences【强连通&&tarjan】
2015-11-07 10:01
501 查看
Proving Equivalences
[b]Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4407 Accepted Submission(s): 1560
[/b]
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2 4 0 3 2 1 2 1 3
[align=left]Sample Output[/align]
4 2
分析:
强连通果题,先压缩路径,把线路转化为点,判断点的出度和入度。
代码:
#include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<stack> #include<vector> using namespace std; #define maxn 20010 vector<int>g[maxn]; stack<int>s; int dfn[maxn],low[maxn],sccno[maxn],tclock,scccnt; int indeg[maxn],outdeg[maxn]; int max(int a,int b) { return a>b?a:b; } void tarjan(int u) { dfn[u]=low[u]=++tclock; s.push(u); for(int i=0;i<g[u].size();i++) { int v=g[u][i]; if(!dfn[v]) { tarjan(v); low[u]=min(low[u],low[v]); } else if(!sccno[v]) { low[u]=min(low[u],dfn[v]); } } if(low[u]==dfn[u]){ scccnt+=1; int v=-1; while(v!=u) { v=s.top(); s.pop(); sccno[v]=scccnt; } } } void findscc(int n) { tclock=scccnt=0; memset(dfn,0,sizeof(int)*(n+1)); memset(sccno,0,sizeof(int)*(n+1)); memset(low,0,sizeof(int)*(n+1)); for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i); } int workout(int n) { if(scccnt==1) return 0; memset(indeg,0,sizeof(int)*(n+1)); memset(outdeg,0,sizeof(int)*(n+1)); for(int u=1;u<=n;u++) { for(int i=0;i<g[u].size();i++) { int v=g[u][i]; if(sccno[u]!=sccno[v]) { indeg[sccno[v]]+=1; outdeg[sccno[u]]+=1; } } } int c1=0,c2=0; for(int i=1;i<=scccnt;i++){ if(indeg[i]==0) c1++; if(outdeg[i]==0) c2++;} return max(c1,c2); } int main() { int t; scanf("%d",&t); while(t--) { int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) g[i].clear(); for(int i=0;i<m;i++) { int a,b; scanf("%d%d",&a,&b); g[a].push_back(b); } findscc(n); printf("%d\n",workout(n)); } return 0; }
相关文章推荐
- IOS UISegmentedControl 分段控制器
- UIScrollView的代理(delegate)
- UIScrollView作为第一个初始化View,导致其内的第一个UIimageView只显示一半的高度
- codeforces-446A-DZY Loves Sequences
- explaining of genotyping and DNA resequencing and variant calling
- SQL Interview Question
- 使用代码提示miui系统开启悬浮框
- Codeforces Round #328 (Div. 2)B. The Monster and the Squirrel
- uinavigationBar常用设置
- UISearchBar custom
- ExecuteThread: '33' for queue: 'weblogic.kernel.Default (self-tuning)'] ERROR
- iOS开发:UIImage赋颜色-----给image赋16进制颜色值
- LightOJ - 1081 Square Queries(二维RMQ)
- LightOJ - 1082 Array Queries(RMQ)
- Easy Guide – Fast Start for sipXecs Admins
- LightOJ - 1048 Conquering Keokradong(贪心)
- Angular.JS + Require.JS + angular-async-loader 来实现异步加载 angular 模块
- lintcode 中等题:Segmemt Tree Build II
- [hadoop2.7.1]I/O之SequenceFile最新API编程实例(写入、读取)
- 【UI基础】时钟效果的简单实现