| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:为什么我写的并查集就不对呢? 牛哥帮小弟看看,小弟第一次发帖, 感谢!In Reply To:为什么我写的并查集就不对呢? 牛哥帮小弟看看,小弟第一次发帖, 感谢! Posted by:mayunfeng at 2009-04-07 20:33:01 > #include<stdio.h>
> #include<algorithm>
> #include<stdlib.h>
> #include<string.h>
> int num[1000001];
> int parent[100001];
> void chushihua(int n)
> {
> int i ;
> for(i = 0 ; i < n; i++){
> parent[i] = i;
> num[i] = 1;
> }
>
> }
>
> void hebin(int x , int y)
> {
> if(num[x] >= num[y]){
> num[x]+=num[y];
> parent[y] = x;
> }
> else{
> num[y]+=num[x];
> parent[x] = y;
> }
> }
>
> int find(int t)
> {
> if(t!=parent[t])
> parent[t] = find(parent[t]);
> return parent[t];
>
>
> }
> int main()
> {
> int count ;
> int n , m ,i ,x, y ,xx, yy , flag;
> int ans = 1;
> scanf("%d",&count);
> while(count --)
> {
> flag = 1;
> scanf("%d%d",&n,&m);
> chushihua(n);
> for(i = 0 ; i < m ; i++)
> {
> scanf("%d%d",&xx,&yy);
> x = find(xx);
> y = find(yy);
>
> if(x == y)
> flag = 0;
> hebin(x, y);
> }
> printf("Scenario #%d:\n",ans++);
> if(flag)
> printf("No suspicious bugs found!\n");
> else
> printf("Suspicious bugs found!\n");
> if(count) printf("\n");
> }
>
> system("pause");
> return 0;
> }
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator