HDU 3836

来源:互联网 发布:武汉seo公司 编辑:程序博客网 时间:2024/06/13 12:56

HDU - 3836
Equivalent Sets
Time Limit: 4000MS Memory Limit: 104857KB 64bit IO Format: %I64d & %I64u

            

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

4 03 21 21 3
 

Sample Output

42

Hint

Case 2: First prove set 2 is a subset of set 1 and then prove set 3 is a subset of set 1.          
 

Source

2011 Multi-University Training Contest 1 - Host by HNU


题意:根据已知的条件,证明任意两个集合相等最少需要几次。
思路:转化为图论问题,其实就是最少再加几条边这个图为一个强连通图。找出所有强连通分量,若连通分量数为1,ans = 0;否则缩点,若要使缩点后的图为强连通图,每个点至少入度和出度都为1,而一条边提供一个入度和一个出度,答案就是入度为0 和 出度为0 的分量数的最大值。
AC代码:
/*~~~~~~~~~~~~~~~HDU 3836   by mowenwen     2015.8.28~~~~~~~~~~~~~~~*/#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define maxn 20000+100#define maxm 50000+100struct Edge{  int next,v;}edge[maxm];int n,m,index,top,tot,scc;int head[maxn],Stack[maxn],dfn[maxn],low[maxn],belong[maxn];int indegree[maxn], outdegree[maxn];bool instack[maxn];int Max(int a,int b){  return a > b? a: b;}void init(){memset(head, -1, sizeof(head));memset(instack, false, sizeof(instack));memset(dfn, -1, sizeof(dfn));memset(low, 0, sizeof(low));memset(indegree, 0, sizeof(indegree));memset(outdegree, 0, sizeof(outdegree));memset(belong, 0,sizeof(belong));tot = 0;index = 0;top = 0;scc = 0;}void addedge(int u,int v){edge[tot].next = head[u];edge[tot].v = v;head[u] = tot ++;}void tarjan(int u){int v;dfn[u] = low[u] = ++index;Stack[top ++] = u;instack[u] = true;for(int i = head[u];i != -1;i = edge[i].next){v = edge[i].v;if(dfn[v] == -1){  tarjan(v);  if(low[v] < low[u])   low[u] = low[v];}else{  if(instack[v] && dfn[v] < low[u])  {    low[u] = dfn[v];  }}}if(dfn[u] == low[u]){scc ++;        int j;do{    j = Stack[--top];instack[j] = false;  //这里写错,wa了快20发,555555belong[j] = scc;}while(j != u);}}void solve(){for(int i = 1;i <= n;i ++){  if(dfn[i] == -1)  tarjan(i);}}int main(){while(scanf("%d%d", &n, &m) != EOF){  init();  while(m --)  {  int a, b;  scanf("%d%d", &a, &b);  addedge(a, b);  }  solve();  for(int i = 1;i <= n;i ++)  {for(int j = head[i];j != -1;j = edge[j].next){int u = i;int v = edge[j].v;if(belong[u] != belong[v]){outdegree[belong[u]] ++;indegree[belong[v]] ++;}}  }  int ans1 = 0, ans2 = 0;  for(int i = 1;i <= scc;i ++)  {if(indegree[i] == 0) ans1++;if(outdegree[i] == 0) ans2++;  }  if(scc == 1 ) printf("0\n");  else  printf("%d\n", Max(ans1, ans2));}    return 0;}

0 0