HDU 1272 小希的迷宫

来源:互联网 发布:趣味数学软件 编辑:程序博客网 时间:2024/06/18 16:22

还是并查集判断环的问题。。。这个题输入弄的很恶心。。。

#include<cstdio>#include<cstring>#include<iostream>#include<iomanip>#include<queue>#include<cmath>#include<stack>#include<map>#include<vector>#include<set>#include<algorithm>using namespace std;typedef long long LL;const int int_max = 0x07777777;const int int_min = 0x80000000;int root[110000];int vis[110000];int x,y;int find (int a){    while(root[a]!=a){        a = root[a];    }    return a;}void combine (int a, int b){    int aa = find(a);    int bb = find(b);    if(aa!=bb){        root[bb] = aa;    }}int main(int argc, const char * argv[]){    while(scanf("%d %d", &x, &y)!=EOF){        if(x==-1 && y==-1) break;        if(x==0 && y==0){            cout << "Yes" << endl;            continue;        }        int result= 0;        int maxn = 0;        for(int i = 0; i < 110000; i++) root[i] = i;        memset(vis, 0, sizeof(vis));        while(x||y){            maxn = (maxn < (x < y ? y : x) ? (x < y ? y : x) : maxn);            vis[x] = 1;            vis[y] = 1;            if(find(x)==find(y)) result = 1;            else combine(x, y);            cin >> x >> y;        }        int count = 0;        for(int i = 1; i <= maxn; i++){            if(vis[i] && root[i]==i) count++;        }        if(result==1) cout << "No" << endl;        else {            if(count!=1) cout << "No" << endl;            else cout << "Yes" << endl;        }    }}


0 0