HDU 3836 Equivalent SetsTarjan+缩点)
2015-06-05 20:41
417 查看
Problem Description
To prove two sets A and B are equivalent, we can first prove A is a subset of B, and then prove B is a subset of A, so finally we got that these two sets are equivalent.
You are to prove N sets are equivalent, using the method above: in each step you can prove a set X is a subset of another set Y, and there are also some sets that are already proven to be subsets of some other sets.
Now you want to know the minimum steps needed to get the problem proved.
Input
The input file contains multiple test cases, in each case, the first line contains two integers N <= 20000 and M <= 50000.
Next M lines, each line contains two integers X, Y, means set X in a subset of set Y.
Output
For each case, output a single integer: the minimum steps needed.
Sample Input
Sample Output
强联通缩点:加入几条边成强联通分量:设缩点后全部点中出度为0的点为d_1,入度为0点为d_2,则答案为max(d_1,d_2);
To prove two sets A and B are equivalent, we can first prove A is a subset of B, and then prove B is a subset of A, so finally we got that these two sets are equivalent.
You are to prove N sets are equivalent, using the method above: in each step you can prove a set X is a subset of another set Y, and there are also some sets that are already proven to be subsets of some other sets.
Now you want to know the minimum steps needed to get the problem proved.
Input
The input file contains multiple test cases, in each case, the first line contains two integers N <= 20000 and M <= 50000.
Next M lines, each line contains two integers X, Y, means set X in a subset of set Y.
Output
For each case, output a single integer: the minimum steps needed.
Sample Input
4 0 3 2 1 2 1 3
Sample Output
4 2 Hint Case 2: First prove set 2 is a subset of set 1 and then prove set 3 is a subset of set 1.
强联通缩点:加入几条边成强联通分量:设缩点后全部点中出度为0的点为d_1,入度为0点为d_2,则答案为max(d_1,d_2);
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<limits.h> typedef long long LL; using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int maxn=20000+100; const int maxm=100000; struct node{ int u,v; int next; }e[maxm]; int head[maxn],cntE; int DFN[maxn],low[maxn]; int s[maxm],top,index,cnt; int belong[maxn],instack[maxn]; int in[maxn],out[maxn]; int n,m; void init() { top=cntE=0; index=cnt=0; CLEAR(DFN,0); CLEAR(head,-1); CLEAR(instack,0); // CLEAR(belong,0); } void addedge(int u,int v) { e[cntE].u=u;e[cntE].v=v; e[cntE].next=head[u]; head[u]=cntE++; } void Tarjan(int u) { DFN[u]=low[u]=++index; instack[u]=1; s[top++]=u; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].v; if(!DFN[v]) { Tarjan(v); low[u]=min(low[u],low[v]); } else if(instack[v]) low[u]=min(low[u],DFN[v]); } int v; if(DFN[u]==low[u]) { cnt++; do{ v=s[--top]; belong[v]=cnt; instack[v]=0; }while(u!=v); } } void work() { REPF(i,1,n) if(!DFN[i]) Tarjan(i); if(cnt<=1) { puts("0"); return ; } CLEAR(in,0); CLEAR(out,0); for(int i=0;i<cntE;i++) { int u=e[i].u,v=e[i].v; if(belong[u]!=belong[v]) in[belong[v]]++,out[belong[u]]++; } int d_1=0,d_2=0; REPF(i,1,cnt) { if(!in[i]) d_1++; if(!out[i]) d_2++; } printf("%d\n",max(d_1,d_2)); } int main() { int u,v; while(~scanf("%d%d",&n,&m)) { init(); for(int i=0;i<m;i++) { scanf("%d%d",&u,&v); addedge(u,v); } work(); } return 0; }
相关文章推荐
- 精美UI欣赏
- java 优先队列 PriorityQueue<E>
- UITableView_6-团购实例,刷新数据,代理实现,GCD方法使用
- Android中的volley_11_Request
- DuiLib : CControlUI->SetAttribute(L"pos"后, 属性不生效的问题
- iOS UILabel显示html标签
- WorldWind源码剖析系列:下载请求类DownloadRequest
- WorldWind源码剖析系列:下载队列类DownloadQueue
- GCD刷新UI
- ios-系统字体一览UIFont
- BZOJ 4059 Cerc2012 Non-boring sequences 线段树+扫描线
- NGUI: UIPanel控件
- VMware GSX Server 3.2.1 Build 19281免费下载
- (转载)UITableView的详细讲解
- GUI练习:列出指定目录内容
- 【转】Compile、Make和Build的区别
- iOS开发系列--UITableView全面解析
- UNIX 高级编程(第二版) apue.h配置问题
- 《大话设计模式》--好菜每回味不同--建造者模式<Builder>(13)
- 全球唯一标识符 System.Guid.NewGuid().ToString()