小鑫的城堡(sdut_2798)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int a[112345];
int hash[112345];
int find(int x)
{
    while(x != a[x]){
        x = a[x];
    }
    return x;
}
void merge(int u, int v)
{
    int f1, f2;
    f1 = find(u);
    f2 = find(v);
    if(f1 != f2){
        a[f1] = f2;
    }
}
int main()
{
    int m;
    while(~scanf("%d", &m)){
        memset(hash,0,sizeof(hash));
        int i;
        for(i = 1;i <= 100000;i++){
            a[i] = i;
        }
        int flag = 1;
        int t = 0;
        int u, v;
        for(i = 0;i < m;i++){
            scanf("%d %d", &u, &v);
            if(hash[u] == 0){
                hash[u] = 1;
                t++;
            }
            if(hash[v] == 0){
                hash[v] = 1;
                t++;
            }
            if(find(u) == find(v)){
                flag = 0;
            }
            t--;
            merge(u,v);
        }
        if(t == 1 && flag == 1){
            printf("Yes\n");
        }else {
            printf("No\n");
        }
    }

    return 0;
}
 

你可能感兴趣的:(小鑫的城堡(sdut_2798))