hdoj2767Proving Equivalences【scc+缩点】
2015-11-20 21:21
381 查看
Proving Equivalences
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4470 Accepted Submission(s): 1577
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
题意 :同hdoj3836
#include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #include<stack> #include<vector> #define inf 0x3f3f3f3f using namespace std; const int maxn=100010; int dfs_clock,scc_cnt; int head[maxn]; int low[maxn]; int dfn[maxn]; int in[maxn]; int out[maxn]; int sccno[maxn]; bool instack[maxn]; stack<int>S; vector<int>scc[maxn]; vector<int>G[maxn]; struct Node{ int from,to,next; }A[maxn]; int MAX(int a,int b){ return a>b?a:b; } int MIN(int a,int b){ return a<b?a:b; } void init(){ dfs_clock=scc_cnt=0; memset(low,0,sizeof(low)); memset(dfn,0,sizeof(dfn)); memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); memset(instack,false,sizeof(instack)); memset(head,-1,sizeof(head)); memset(sccno,0,sizeof(sccno)); } void tarjan(int u,int pre){ int v; dfn[u]=low[u]=++dfs_clock; instack[u]=true;S.push(u); for(int k=head[u];k!=-1;k=A[k].next){ v=A[k].to; if(!dfn[v]){ tarjan(v,u); low[u]=MIN(low[u],low[v]); } else if(instack[v]){ low[u]=MIN(low[u],dfn[v]); } } if(low[u]==dfn[u]){ scc_cnt++;scc[scc_cnt].clear(); G[scc_cnt].clear(); while(1){ v=S.top();S.pop(); instack[v]=false; sccno[v]=scc_cnt; scc[scc_cnt].push_back(v); if(u==v)break; } } } void suodian(int m){ for(int i=0;i<m;++i){ int u=sccno[A[i].from]; int v=sccno[A[i].to]; if(u!=v){ G[u].push_back(v); in[v]++;out[u]++; } } } int main() { int i,j,k,n,m,t; scanf("%d",&t); while(t--){ init(); scanf("%d%d",&n,&m); for(i=0;i<m;++i){ scanf("%d%d",&A[i].from,&A[i].to); A[i].next=head[A[i].from]; head[A[i].from]=i; } for(i=1;i<=n;++i){ if(!dfn[i]){ tarjan(i,-1); } } if(scc_cnt==1){ printf("%d\n",0); continue; } suodian(m); int ans1=0,ans2=0; for(i=1;i<=scc_cnt;++i){ if(in[i]==0)ans1++; if(out[i]==0)ans2++; } printf("%d\n",MAX(ans1,ans2)); } return 0; }
相关文章推荐
- 独立使用UEditor编辑器图片和文件上传(调用内置第三方插件swfupload)
- iOS开发系列-- UIScrollView
- IOS之UI--自定义按钮实现代理监听点击事件
- PyQt4(使用ui)
- 注解(浅谈Dagger,ButterKnife,Roboguide)
- APUE学习笔记——标准I/O
- UIDatePicker 日期滚轮)/时间选取器
- 提高Interface Builder高效工作的8个技巧
- UIImagePickerController 的部分用法详解
- leetcode-implement-queue-using-stacks
- iOS中UIMenuController的使用
- UIScrollView与UIPageControl+自动滑动的封装
- ios UILocalNotification的使用
- Qt .ui转换为.h文件
- STL容器-序列式容器deque
- Android——使用Handle和Message更新UI控件
- iOS开发中UIViewcontentMode的12种属性值
- ios的手势UIGestureRecognizer
- Android中UI线程与后台线程交互设计的5种方法
- 【iOS功能实现】之利用UIDocumentInteractionController打开和预览文档