转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
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也是同理。
此外,维护好每一个集合中的节点的数目,取非的不要放到算入计数。
然后枚举每个点,看他们所在的集合,取一个较大值。注意不要重复取
//#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
const int MAXN = ;
int pa[MAXN*];
int ra[MAXN*];
void init(int n){
for(int i=;i<n;i++){
pa[i] = i;
ra[i] = ;
}
}
int find(int x){
if(pa[x]!=x)pa[x] = find(pa[x]);
return pa[x];
}
int unite(int x,int y){
x = find(x);
y = find(y);
if(x==y)return ;
if(ra[x]<ra[y]){
pa[x] = y;
}else{
pa[y] = x;
if(ra[x]==ra[y])ra[x]++;
}
return ;
}
bool same(int x,int y){
return find(x) == find(y);
}
int used[MAXN*];
int num[MAXN*];
int vis[MAXN*];
int main()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
int cas = ;
while(t--){
int n;
cin>>n;
init(MAXN*);
int u,v;
memset(used,,sizeof(used));
for(int i=;i<n;i++){
cin>>u>>v;
u--;v--;
used[u] = used[v] = ;
unite(u,v+MAXN);
unite(v,u+MAXN);
}
memset(num,,sizeof(num));
for(int i=;i<MAXN;i++){
if(used[i]){
int fa = find(i);
num[fa]++;
}
}
int x,y;
memset(vis,,sizeof(vis));
int ans = ;
for(int i=;i<MAXN;i++){
if(used[i]){
x = find(i);
y = find(i+MAXN);
if(vis[x]||vis[y])continue;
ans += max(num[x],num[y]);
vis[x] = vis[y] = ;
}
}
cout<<"Case "<<cas++<<": "<<ans<<endl;
} return ;
}