首页 > 代码库 > HDU 3836 Equivalent Sets
HDU 3836 Equivalent Sets
Equivalent Sets
Time Limit: 4000ms
Memory Limit: 104857KB
This problem will be judged on HDU. Original ID: 383664-bit integer IO format: %I64d Java class name: Main
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.
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.
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
解题:本意就是最少添加多少条边,使得全图强连通。tarjan求极大强连通子图后缩点。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 20100;18 int dfn[maxn],low[maxn],belong[maxn];19 bool instack[maxn];20 vector<int>g[maxn];21 stack<int>stk;22 int n,m,cnt,scc,in[maxn],out[maxn];23 void tarjan(int u){24 dfn[u] = low[u] = ++cnt;25 stk.push(u);26 instack[u] = true;27 for(int i = 0; i < g[u].size(); i++){28 if(!dfn[g[u][i]]){29 tarjan(g[u][i]);30 low[u] = min(low[u],low[g[u][i]]);31 }else if(instack[g[u][i]]) low[u] = min(low[u],dfn[g[u][i]]);32 }33 if(dfn[u] == low[u]){34 scc++;35 int v;36 do{37 v = stk.top();38 instack[v] = false;39 belong[v] = scc;40 stk.pop();41 }while(v != u);42 }43 }44 int main() {45 int i,j,u,v,a,b;46 while(~scanf("%d %d",&n,&m)){47 for(i = 0; i <= n; i++){48 dfn[i] = low[i] = belong[i] = 0;49 instack[i] = false;50 g[i].clear();51 in[i] = out[i] = 0;52 }53 cnt = scc = 0;54 while(!stk.empty()) stk.pop();55 for(i = 1; i <= m; i++){56 scanf("%d %d",&u,&v);57 g[u].push_back(v);58 }59 for(i = 1; i <= n; i++)60 if(!dfn[i]) tarjan(i);61 for(i = 1; i <= n; i++){62 for(j = 0; j < g[i].size(); j++){63 if(belong[i] != belong[g[i][j]]){64 in[belong[g[i][j]]]++;65 out[belong[i]]++;66 }67 }68 }69 for(a = b = 0,i = 1; i <= scc; i++){70 if(!in[i]) a++;71 if(!out[i]) b++;72 }73 printf("%d\n",scc == 1?0:max(a,b));74 }75 return 0;76 }
HDU 3836 Equivalent Sets
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。