2 8 5 3 4 5 6 1 2 5 8 3 5 5 4 2 3 4 5 3 4 2 4
5 3 Poor wyh
//468MS 4036K 1981 B C++
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
using namespace std;
typedef long long ll;
#define X first
#define Y second
typedef pair<ll,ll> pii;
const int N = 1e5+100;
int sz[N*2];
int sz2[N*2];
int fa[N*2];
int n,m;
void ini(){
REP(i,2*n) fa[i] = i;
REP(i,2*n) sz[i] = (i <= n);
REP(i,2*n) sz2[i] = (i > n);
}
int getf(int x){
return x == fa[x] ? x : fa[x] = getf(fa[x]);
}
bool same(int a,int b){
return getf(a) == getf(b);
}
void Merge(int a,int b){
int f1 = getf(a), f2 = getf(b);
if(f1 == f2) return ;
fa[f1] = f2;
sz[f2] += sz[f1];
sz2[f2] += sz2[f1];
}
int main(){
int T;
cin>>T;
while(T--){
scanf("%d%d",&n,&m);
ini();
bool flag = 0;
REP(i,m){
int a,b;
scanf("%d%d",&a,&b);
if(flag) continue;
if(same(a,b) || same(a+n,b+n) ) flag = 1;
else Merge(a+n,b),Merge(a,b+n);
}
if( n < 2 || flag) puts("Poor wyh");
else if(m == 0) printf("%d 1\n",n-1);
else {
int ans = 0;
REP(i,n){
if( fa[i] == i) ans += min(sz[i],sz2[i]);
}
printf("%d %d\n",n-ans,ans);
}
}
}
HDU 5285 wyh2000 and pupil(dfs或种类并查集)
原文:http://www.cnblogs.com/gavanwanggw/p/6819448.html