Reprint Please specify source: 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 is fighting each other to death. The war has become so fierce that, none of knows who would win. The humans want to know who'll survive finally. But humans is 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 the 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 is contains an integer n (1≤n≤105), denoting the number of dual fights. Each of the next n lines would contain the different integers u v (1≤u, v≤20000) denoting there was a Fight between u and v. No rival would 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
Ask the number of the larger of the two races what the maximum possible is.
A typical take-right and check-set.
It is known that you and V are not in a race, and the means to deal with it is to put u and non-V in a set and put V and non-u into a set. Using U+MAXN to represent non-u, non-V is also the same.
In addition, maintain the number of nodes in each collection, and do not put the count into counting.
Then enumerate each point to see where they are in the collection and take a larger value. Why this can be taken, roughly according to the two-dimensional graph dyeing ... Not much to say the clear
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>Ten#include <list> One#include <queue> A#include <deque> -#include <stack> -#include <Set> the#include <map> -#include <cstdio> -#include <cstdlib> -#include <cmath> +#include <cstring> -#include <climits> +#include <cctype> A using namespacestd; at #defineXinf Int_max - #defineINF 0X3FFFFFFF - #defineMP (x, y) make_pair (x, y) - #definePB (x) push_back (x) - #defineREP (x,n) for (int x=0; x<n; X + +) - #defineREP2 (X,L,R) for (int x=l; x<=r; X + +) in #defineDEP (x,r,l) for (int x=r; x>=l; x--) - #defineCLR (a,x) memset (a,x,sizeof (A)) to #defineIT iterator +typedefLong Longll; -typedef pair<int,int>PII; thetypedef vector<pii>VII; *typedef vector<int>VI; $ Const intMAXN =20000;Panax Notoginseng intvis[maxn*2+Ten]; - BOOLused[maxn*2+Ten]; the intpa[maxn*2+Ten]; + intra[maxn*2+Ten]; A intFindintx) { the if(pa[x]!=x) pa[x]=find (Pa[x]); + returnPa[x]; - } $ voidUniteintXinty) { $x =find (x); -y =find (y); - if(x==y)return; thepa[y]=x; -ra[x]+=Ra[y];Wuyi } the voidinit () { -CLR (RA,0); Wu for(intI=0; i<=maxn+maxn;i++) pa[i]=i; -CLR (Used,0); AboutCLR (Vis,0); $ } - intMain () - { -Ios::sync_with_stdio (false); A intT; +Cin>>T; the intcas=1; - while(t--){ $ intN; theCin>>N; the intu,v; the init (); the for(intI=0; i<n;i++){ -Cin>>u>>v; in if(!ra[u]) ra[u]=1; the if(!ra[v]) ra[v]=1; theused[u]=used[v]=1; AboutUnite (u,v+MAXN); theUnite (u+maxn,v); the } the intans=0; + for(intI=1; i<=maxn;i++){ - if(Used[i]) { the intx =find (i);Bayi inty = Find (i+MAXN); the if(vis[x]| | Vis[y])Continue; theans+=Max (ra[x],ra[y]); -vis[x]=vis[y]=1; - } the } thecout<<" Case"<<cas++<<": "<<ans<<Endl; the } the return 0; -}
code June
Lightoj1009 back to Underworld (with right and check set)