1, hdu1232
2, Link: http://acm.hdu.edu.cn/showproblem.php?pid=1232
3, Summary: Simple and check set
#include <iostream>#include<cstring>#include<cmath>#include<queue>#include<algorithm>#include<cstdio>using namespacestd;#defineLL Long Long#defineINF 0x3f3f3f3fConst intn=1010;intFather[n];/*//non-optimized lookup int findn (int n) {while (father[n]!=n) {n=father[n]; } return n;} */intFINDN (intN) { intx=N; while(father[x]!=x) {x=Father[x]; } intI=n,j; while(father[i]!=x)//Optimized{J=Father[i]; Father[i]=x; I=J; } returnx;}intMain () {intn,m; while(SCANF ("%d", &n)!=eof&&N) {//Initialize for(intI=1; i<=n;i++) Father[i]=i; scanf ("%d",&m); intx, y; for(intI=0; i<m;i++) {scanf ("%d%d",&x,&y); //Merging inttempx=findn (x); inttempy=Findn (y); if(tempx!=tempy) father[tempx]=Tempy; } //find out the number of connected components, minus one intnum=0; for(intI=1; i<=n;i++) { if(father[i]==i) num++; } printf ("%d\n", num-1); } return 0;}View Code
hdu1232 and check Set