Equivalent sets
Problem Descriptionto prove-sets A and B are equivalent, we can first prove a was a subset of B, and then prove B is a Subset of A, so-finally we got that these and sets are equivalent.
You be to prove N sets be equivalent, using the method above:in each step can prove a set X is a subset of another Set Y, and there is also some sets that is already proven to being subsets of some other sets.
Now, want to know the minimum steps needed to get the problem proved.
Inputthe input file contains multiple test cases, in each case, the first line contains both integers N <= 20000 and M & lt;= 50000.
Next M lines, each line contains the integers x, Y, means set X in a subset of set Y.
Outputfor, output a single integer:the minimum steps needed.
Sample Input4 03 21 21 3
Sample Output42
HintCase 2:first prove set 2 are a subset of Set 1 and then prove set 3 is a subset of Set 1.
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <stack>5#include <vector>6 using namespacestd;7 8 Const intmaxn=20005;9vector<int>G[MAXN];Tenstack<int>s; One int inch[MAXN], out[MAXN],DFN[MAXN],LOWLINK[MAXN],SCCNO[MAXN]; A intScc_cnt,dfs_clock; - intM,n; - the voidInit () - { - for(intI=1; i<=n;i++) g[i].clear (); -Memsetinch,0,sizeof(inch)); +Memset out,0,sizeof( out)); -memset (DFN,0,sizeof(DFN)); +memset (Lowlink,0,sizeof(Lowlink)); Amemset (Sccno,0,sizeof(SCCNO)); atscc_cnt=dfs_clock=0; - } - - voidTarjan (intu) - { -lowlink[u]=dfn[u]=++Dfs_clock; in s.push (u); - for(intI=0; I<g[u].size (); i++) to { + intv=G[u][i]; - if(!Dfn[v]) the { * Tarjan (v); $lowlink[u]=min (lowlink[u],lowlink[v]);Panax Notoginseng } - Else if(!Sccno[v]) thelowlink[u]=min (lowlink[u],dfn[v]); + } A if(lowlink[u]==Dfn[u]) the { +scc_cnt++; - while(1) $ { $ intx=s.top (); - S.pop (); -sccno[x]=scc_cnt; the if(X==u) Break; - }Wuyi } the } - Wu - About intMain () $ { - while(SCANF ("%d%d", &n,&m)! =EOF) - { - init (); A for(intI=0; i<m;i++) + { the intu,v; -scanf"%d%d",&u,&v); $ G[u].push_back (v); the } the for(intI=1; i<=n;i++) the if(!Dfn[i]) the Tarjan (i); - for(intI=1; i<=n;i++) in for(intj=0; J<g[i].size (); j + +) the if(sccno[g[i][j]]!=Sccno[i]) the { About inch[sccno[g[i][j]]]++; the out[sccno[i]]++; the } the intcnt1=0, cnt2=0; + for(intI=1; i<=scc_cnt;i++) - { the if(!inch[i])Bayicnt1++; the if(! out[i]) thecnt2++; - } - if(scc_cnt==1) thePuts"0"); the Else theprintf"%d\n", Max (Cnt1,cnt2)); the } - return 0; the}
HDU 3836 equivalent sets (strong connected pinch point)