并查集就是把不同的放到一个集合之中
递归
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
int find(int x) { if(x!=pa[x]) pa[x]=find(pa[x]); return pa[x]; } void union(int x,int y) { int fx,fy; fx=find(x); fy=find(y); if(fx!=fy) pa[fx]=fy; }
非递归
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 int find(int x) 2 { 3 int i,j,root; 4 root=x; 5 while(root!=pa[root]) 6 root=pa[root]; 7 i=x; 8 while(pa[i]!=root) 9 { 10 j=pa[i]; 11 pa[i]=root; 12 i=j; 13 } 14 return r; 15 } 16 void union(int x,int y) 17 { 18 int fx,fy; 19 fx=find(x); 20 fy=find(y); 21 if(fx!=fy) 22 { 23 pa[fx]=fy; 24 } 25 }
hdu1232
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<string.h> 3 int pa[1002]; 4 void init(int n) 5 { 6 int i; 7 for(i=0;i<=n;i++) 8 { 9 pa[i]=i; 10 } 11 } 12 int find(int x) 13 { 14 int i,j,r; 15 r=x; 16 while(r!=pa[r]) 17 r=pa[r]; 18 i=x; 19 while(pa[i]!=r) 20 { 21 j=pa[i]; 22 pa[i]=r; 23 i=j; 24 } 25 return r; 26 } 27 int main() 28 { 29 int i,j,n,m,ans; 30 while(scanf("%d",&n)!=EOF) 31 { 32 if(n==0) 33 break; 34 scanf("%d",&m); 35 init(n); 36 ans=0; 37 for(i=0;i<m;i++) 38 { 39 int x,y; 40 scanf("%d%d",&x,&y); 41 int fx=find(x); 42 int fy=find(y); 43 if(fx!=fy) 44 { 45 pa[fx]=fy; 46 } 47 } 48 for(i=1;i<=n;i++) 49 { 50 if(pa[i]==i) 51 ans++; 52 } 53 printf("%d ",ans-1); 54 } 55 }
poj1308
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<string.h> 3 int pa[1010],a[1010],b[1010],n,in[1010],map[1010],hash[1010]; 4 void init() 5 { 6 for(int i=0;i<=n;i++) 7 { 8 pa[i]=i; 9 } 10 } 11 int find(int x) 12 { 13 if(x!=pa[x]) 14 pa[x]=find(pa[x]); 15 return pa[x]; 16 } 17 int main() 18 { 19 int i,j,flag,ff=0; 20 while(scanf("%d%d",&a[0],&b[0])!=EOF) 21 { 22 j=0; 23 memset(hash,0,sizeof(hash)); 24 memset(map,0,sizeof(map)); 25 memset(in,0,sizeof(in)); 26 if(!hash[a[0]]) 27 map[j++]=a[0]; 28 if(!hash[b[0]]) 29 map[j++]=b[0]; 30 if(a[0]==0&&b[0]==0) 31 { 32 printf("Case %d is a tree. ",++ff); 33 continue; 34 } 35 n=0; 36 flag=1; 37 if(a[0]<0&&b[0]<0) 38 break; 39 in[a[0]]++,in[b[0]]++; 40 if(a[0] > n) n = a[0]; 41 if(b[0] > n) n = b[0]; 42 i=0; 43 while(1) 44 { 45 i++; 46 scanf("%d %d",&a[i],&b[i]); 47 if(a[i]==0&&b[i]==0) 48 break; 49 if(!hash[a[i]]) 50 { 51 hash[a[i]]=1; 52 map[j++]=a[i]; 53 } 54 if(!hash[b[i]]) 55 { 56 map[j++]=b[i]; 57 hash[b[i]]=1; 58 } 59 if(a[i] > n) n = a[i]; 60 if(b[i] > n) n = b[i]; 61 } 62 int num=j; 63 //for(j=0;j<num;j++) 64 // printf("%d ",map[j]); 65 //printf(" "); 66 int len=i; 67 init(); 68 for(i=0;i<len;i++) 69 { 70 int x,y; 71 x=find(a[i]); 72 y=find(b[i]); 73 if(x!=y) 74 { 75 pa[x]=y; 76 } 77 else 78 flag=0; 79 } 80 int ans=0; 81 for(i=0;i<num;i++) 82 { 83 if(pa[map[i]]==map[i]) 84 { 85 ans++; 86 } 87 } 88 if(ans>1) 89 flag=0; 90 if(flag) 91 printf("Case %d is a tree. ",++ff); 92 else 93 printf("Case %d is not a tree. ",++ff); 94 } 95 }
hdu1856
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<string.h> 3 int pa[10000020],count[10000020],maxn; 4 int a[100020],b[100020]; 5 void init(int m) 6 { 7 for(int i=0;i<=m;i++) 8 { 9 pa[i]=i; 10 count[i]=1; 11 } 12 } 13 int find(int x) 14 { 15 if(x!=pa[x]) 16 pa[x]=find(pa[x]); 17 return pa[x]; 18 } 19 int main() 20 { 21 int i,j,n,m; 22 while(scanf("%d",&n)!=EOF) 23 { 24 if(n==0) 25 { 26 printf("%d ",1); 27 continue; 28 } 29 maxn=0; 30 m=0; 31 for(i=0;i<n;i++) 32 { 33 scanf("%d%d",&a[i],&b[i]); 34 if(a[i] > m) m = a[i]; 35 if(b[i] > m) m = b[i]; 36 } 37 38 init(m); 39 40 for(i=0;i<n;i++) 41 { 42 int l1,l2; 43 l1=find(a[i]); 44 l2=find(b[i]); 45 if(l1!=l2) 46 { 47 pa[l1]=l2; 48 int z1,z2; 49 z1=count[l1]; 50 z2=count[l2]; 51 count[l1]+=z2; 52 count[l2]+=z1; 53 if(count[l1]>maxn) 54 maxn=count[l1]; 55 if(count[l2]>maxn) 56 maxn=count[l2]; 57 } 58 } 59 printf("%d ",maxn); 60 } 61 }
hdu1198
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<string.h> 3 struct node 4 { 5 int left; 6 int right; 7 int donw; 8 int up; 9 }t[20]; 10 struct NODE 11 { 12 int val; 13 int num; 14 }; 15 char s[60][60]; 16 NODE map[60][60]; 17 int pa[2600],n,m; 18 void init(int x) 19 { 20 for(int i=0;i<=x;i++) 21 pa[i]=i; 22 } 23 void biao() 24 { 25 t[1].up=1;t[1].left=1;t[1].donw=t[1].right=0; 26 t[2].up=t[2].right=1;t[2].donw=t[2].left=0; 27 t[3].left=t[3].donw=1;t[3].right=t[3].up=0; 28 t[4].donw=t[4].right=1;t[4].left=t[4].up=0; 29 t[5].up=t[5].donw=1;t[5].left=t[5].right=0; 30 t[6].left=t[6].right=1;t[6].donw=t[6].up=0; 31 t[7].donw=0;t[7].up=t[7].left=t[7].right=1; 32 t[8].right=0;t[8].up=t[8].donw=t[8].left=1; 33 t[9].up=0;t[9].donw=t[9].left=t[9].right=1; 34 t[10].left=0;t[10].donw=t[10].right=t[10].up=1; 35 t[11].donw=t[11].left=t[11].right=t[11].up=1; 36 } 37 int find(int x) 38 { 39 if(x!=pa[x])pa[x]=find(pa[x]); 40 return pa[x]; 41 } 42 void makemap(int i,int j,int x,int y,int set) 43 { 44 if(x<0||y<0||x>=n||y>=m) 45 return; 46 int a,b,l1,l2; 47 a=map[i][j].val;b=map[x][y].val; 48 if(set==1) 49 { 50 if(t[a].right&&t[b].left) 51 { 52 l1=find(map[i][j].num); 53 l2=find(map[x][y].num); 54 if(l1!=l2) 55 pa[l1]=l2; 56 } 57 } 58 else if(set==2) 59 { 60 if(t[a].up&&t[b].donw) 61 { 62 l1=find(map[i][j].num); 63 l2=find(map[x][y].num); 64 if(l1!=l2) 65 pa[l1]=l2; 66 } 67 } 68 else if(set==3) 69 { 70 if(t[a].left&&t[b].right) 71 { 72 l1=find(map[i][j].num); 73 l2=find(map[x][y].num); 74 if(l1!=l2) 75 pa[l1]=l2; 76 } 77 } 78 else if(set==4) 79 { 80 if(t[a].donw&&t[b].up) 81 { 82 l1=find(map[i][j].num); 83 l2=find(map[x][y].num); 84 if(l1!=l2) 85 pa[l1]=l2; 86 } 87 } 88 } 89 int main() 90 { 91 int i,j; 92 biao(); 93 while(scanf("%d%d",&n,&m)!=EOF) 94 { 95 if(n<0&&m<0) 96 break; 97 98 init(n*m); 99 for(i=0;i<n;i++) 100 scanf("%s",s[i]); 101 int count=0; 102 for(i=0;i<n;i++) 103 for(j=0;j<m;j++) 104 { 105 map[i][j].val=s[i][j]-'A'+1; 106 map[i][j].num=++count; 107 } 108 109 for(i=0;i<n;i++) 110 { 111 for(j=0;j<m;j++) 112 { 113 makemap(i,j,i,j+1,1);//1right 114 makemap(i,j,i,j-1,3);//3left 115 makemap(i,j,i-1,j,2);//2up 116 makemap(i,j,i+1,j,4);//4down 117 } 118 } 119 int ans=0; 120 for(i=1;i<=n*m;i++) 121 { 122 if(pa[i]==i) 123 ans++; 124 } 125 printf("%d ",ans); 126 } 127 } 128 /* 129 2 3 130 AAC 131 FFG 132 */