九度:1019<图的连通性>

来源:互联网 发布:免费永久域名 编辑:程序博客网 时间:2024/06/12 08:34




// 吉林大学11机试// 九度:1019// 并查集判断无向图是否连通#include <stdio.h>#define SIZE 1005int fa[SIZE];int sum[SIZE];int Getfa(int x){    if(x == fa[x])        return x;    else        fa[x] = Getfa(fa[x]);    return fa[x];}void Init(){    for(int i=0; i<SIZE; i++)    {        fa[i]=i;        sum[i]=1;    }}int main(){#ifdef ONLINE_JUDGE#elsefreopen("E:\\in.txt", "r", stdin);//freopen("E:\\out.txt", "w", stdout);#endif    int n, m;    while(scanf("%d%d", &n ,&m) && n)    {        Init();        int x, y;        int a, b;        while(m-->0)        {            scanf("%d%d", &x, &y);            a=Getfa(x);            b=Getfa(y);            if(a != b)            {                fa[a]=b;                sum[b]+=sum[a];            }        }        //printf("%d\n", sum[b]);        puts(sum[b] == n? "YES":"NO");    }    return 0;}


0 0