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

Lightoj1009 Back to Underworld(带权并查集)

时间:2015-07-21 23:58:15      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

转载请注明出处: http://www.cnblogs.com/fraud/           ——by fraud

 

Back to Underworld
Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

The Vampires and Lykans are fighting each other to death. The war has become so fierce that, none knows who will win. The humans want to know who will survive finally. But humans are afraid of going to the battlefield.

So, they made a plan. They collected the information from the newspapers of Vampires and Lykans. They found the information about all the dual fights. Dual fight means a fight between a Lykan and a Vampire. They know the name of the dual fighters, but don‘t know which one of them is a Vampire or a Lykan.

So, the humans listed all the rivals. They want to find the maximum possible number of Vampires or Lykans.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case contains an integer n (1 ≤ n ≤ 105), denoting the number of dual fights. Each of the next n lines will contain two different integers u v (1 ≤ u, v ≤ 20000) denoting there was a fight between u and v. No rival will be reported more than once.

Output

For each case, print the case number and the maximum possible members of any race.

Sample Input

2

2

1 2

2 3

3

1 2

2 3

4 2

Sample Output

Case 1: 2

Case 2: 3

问其中两个种族的数目较大的那个值最大可能是多少。

典型的带权并查集。

已知u和v不在一个种族,所需处理的手段就是,把u和非v放到一个集合,把v和非u放到一个集合。利用u+MAXN来表示非u,非v也是同理。

此外,维护好每一个集合中的节点的数目,取非的不要放到算入计数。

然后枚举每个点,看他们所在的集合,取一个较大值。为什么这样取可以,大致根据二分图染色。。。不大说的清

技术分享
 1 //#pragma comment(linker, "/STACK:102400000,102400000")
 2 #include <iostream>
 3 #include <sstream>
 4 #include <ios>
 5 #include <iomanip>
 6 #include <functional>
 7 #include <algorithm>
 8 #include <vector>
 9 #include <string>
10 #include <list>
11 #include <queue>
12 #include <deque>
13 #include <stack>
14 #include <set>
15 #include <map>
16 #include <cstdio>
17 #include <cstdlib>
18 #include <cmath>
19 #include <cstring>
20 #include <climits>
21 #include <cctype>
22 using namespace std;
23 #define XINF INT_MAX
24 #define INF 0x3FFFFFFF
25 #define MP(X,Y) make_pair(X,Y)
26 #define PB(X) push_back(X)
27 #define REP(X,N) for(int X=0;X<N;X++)
28 #define REP2(X,L,R) for(int X=L;X<=R;X++)
29 #define DEP(X,R,L) for(int X=R;X>=L;X--)
30 #define CLR(A,X) memset(A,X,sizeof(A))
31 #define IT iterator
32 typedef long long ll;
33 typedef pair<int,int> PII;
34 typedef vector<PII> VII;
35 typedef vector<int> VI;
36 const int MAXN = 20000;
37 int vis[MAXN*2+10];
38 bool used[MAXN*2+10];
39 int pa[MAXN*2+10];
40 int ra[MAXN*2+10];
41 int find(int x){
42     if(pa[x]!=x)pa[x]=find(pa[x]);
43     return pa[x];
44 }
45 void unite(int x,int y){
46     x = find(x);
47     y = find(y);
48     if(x==y)return;
49     pa[y]=x;
50     ra[x]+=ra[y];
51 }
52 void init(){
53     CLR(ra,0);
54     for(int i=0;i<=MAXN+MAXN;i++)pa[i]=i;
55     CLR(used,0);
56     CLR(vis,0);
57 }
58 int main()
59 {
60     ios::sync_with_stdio(false);
61     int t;
62     cin>>t;
63     int cas=1;
64     while(t--){
65         int n;
66         cin>>n;
67         int u,v;
68         init();
69         for(int i=0;i<n;i++){
70             cin>>u>>v;
71             if(!ra[u])ra[u]=1;
72             if(!ra[v])ra[v]=1;
73             used[u]=used[v]=1;
74             unite(u,v+MAXN);
75             unite(u+MAXN,v);
76         }
77         int ans=0;
78         for(int i=1;i<=MAXN;i++){
79             if(used[i]){
80                 int x = find(i);
81                 int y = find(i+MAXN);
82                 if(vis[x]||vis[y])continue;
83                 ans+=max(ra[x],ra[y]);
84                 vis[x]=vis[y]=1;
85             }
86         }
87         cout<<"Case "<<cas++<<": "<<ans<<endl;
88     }
89     return 0;
90 }
代码君

 

Lightoj1009 Back to Underworld(带权并查集)

标签:

原文地址:http://www.cnblogs.com/fraud/p/4665866.html

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