并查集

uva 1197 

#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#define  LL long long
#define  ULL unsigned long long
#define mod 1000000007
#define INF 0x7ffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define MODD(a,b) (((a%b)+b)%b)
using namespace std;
const int N=35005;
int p[N],num[N],a[N];
int n,m;
int findfa(int x)
{
    if(x!=p[x])
        p[x]=findfa(p[x]);
    return p[x];

}
void Union(int x,int y)
{

    int fax=findfa(x),fay=findfa(y);
    if(fax!=fay){
        p[fay]=fax;
        num[fax]+=num[fay];
    }
    return ;
}
int main()
{

   while(scanf("%d%d",&n,&m),n||m){
    for(int i=0;i<n;i++) p[i]=i,num[i]=1;
    while(m--){
        int x;
        scanf("%d",&x);
        for(int i=0;i<x;i++) scanf("%d",&a[i]);
        for(int i=0;i<x-1;i++) Union(a[i],a[i+1]);
    }
    int sum=findfa(0);
    printf("%d\n",num[sum]);
   // mem(p,0),mem(num,0),mem(a,0);
   }

    return 0;
}

 

 

hdu1272

#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#define  LL long long
#define  ULL unsigned long long
#define mod 1000000007
#define INF 0x7ffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define MODD(a,b) (((a%b)+b)%b)
using namespace std;
const int N=100005;
int p[N],r[N];

int findfa(int x)
{
    if(x!=p[x])
       p[x]=findfa(p[x]);


    return p[x];
}
int Union(int x,int y)
{
    int fax=findfa(x),fay=findfa(y);
    if(fax==fay) return 1;
    p[fay]=fax;
    return 0;

}
int main()
{
    int a,b,cot=0,ans=0;
    for(int i=0;i<=N;i++) p[i]=i;
    mem(r,0);
    while(~scanf("%d",&a)){
        scanf("%d",&b);
        if(a==0&&b==0){

            for(int i=1;i<=N;i++){
                if(p[i]==i&&r[i])
                    ans++;
            }
            if(ans==0&&a==0&&b==0) r[a]++,r[b]++;
            if((!cot&&ans==1)||(r[0]==2)) printf("Yes\n");
            else if(cot||ans>1) printf("No\n");
            for(int i=1;i<=N;i++) p[i]=i;
            cot=0,ans=0;
            mem(r,0);
            continue;
        }
        else if(a==-1&&b==-1) return 0;
        r[a]++,r[b]++;
        if(cot!=1) cot=Union(a,b);
        else Union(a,b);


    }



    return 0;


}



hdu1231


 
Online Judge	Online Exercise	Online Teaching	Online Contests	Exercise Author
F.A.Q
Hand In Hand
Online Acmers	
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist

 	
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests
Author yangftc
Mail Mail 0(0)
Control Panel Control Panel 
Sign Out Sign Out
View Code
Problem : 1213 ( How Many Tables )     Judge Status : Accepted
RunId : 27165582    Language : G++    Author : yangftc
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#define  LL long long
#define  ULL unsigned long long
#define mod 1000000007
#define INF 0x7ffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define MODD(a,b) (((a%b)+b)%b)
using namespace std;
const int N=1000005;
int p[N],r[N];

int findfa(int x)
{
    if(x!=p[x]){
        int fa=findfa(p[x]);

        p[x]=fa;
    }
    return p[x];
}
void Union(int x,int y)
{
    int fax=findfa(x),fay=findfa(y);
    if(fax==fay){
       return ;
    }
    p[fay]=fax;
    r[fay]+=r[fax];

}
int main()
{
   int T;
   int cot=1;
   scanf("%d",&T);
   while(T--){
    int n,m;

    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) p[i]=i,r[i]=1;

    for(int i=0;i<m;i++){
        int x,y;
        scanf("%d%d",&x,&y);

        Union(x,y);

    }
    int maxn=0;
    for(int i=1;i<=n;i++){
        if(p[i]==i) maxn++;
    }
    printf("%d\n",maxn);
   }


    return 0;


}



[ Copy to Clipboard ]    [ Save to File]



Home | Top	Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2018 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2018-11-13 22:53:56, Gzip enabled	Administration

 hdu 1829

#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#define  LL long long
#define  ULL unsigned long long
#define mod 1000000007
#define INF 0x7ffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define MODD(a,b) (((a%b)+b)%b)
using namespace std;
const int N=1000005;
int p[N],r[N];
bool flag;
int findfa(int x)
{
    if(x!=p[x]){
        int fa=findfa(p[x]);

        r[x]=(r[p[x]]+r[x])&1;
        p[x]=fa;
    }
    return p[x];
}
void Union(int x,int y)
{
    int fax=findfa(x),fay=findfa(y);
    if(fax==fay){
        if(r[x]==r[y]) flag=true;
        return ;
    }
    p[fay]=fax;
    r[fay]=(r[x]+1+r[y])&1;

}
int main()
{
   int T;
   int cot=1;
   scanf("%d",&T);
   while(T--){
    int n,m;
    flag=false;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) p[i]=i,r[i]=0;

    for(int i=0;i<m;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        if(flag) continue;
        Union(x,y);

    }
    printf("Scenario #%d:\n",cot++);
    if(flag) printf("Suspicious bugs found!\n");
    else printf("No suspicious bugs found!\n");
    printf("\n");
   }

    return 0;


}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值