码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3836 Equivalent Sets

时间:2014-08-25 21:08:34      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   os   io   strong   for   

Equivalent Sets

Time Limit: 4000ms
Memory Limit: 104857KB
This problem will be judged on HDU. Original ID: 3836
64-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.
 

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.

Source

 
解题:本意就是最少添加多少条边,使得全图强连通。tarjan求极大强连通子图后缩点。
 
bubuko.com,布布扣
 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 long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 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 }
View Code

 

HDU 3836 Equivalent Sets

标签:style   blog   http   color   java   os   io   strong   for   

原文地址:http://www.cnblogs.com/crackpotisback/p/3935773.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!