[luogu2307]迷宫(并查集)

来源:互联网 发布:java 创建服务 编辑:程序博客网 时间:2024/06/06 05:47

INPUT
6 8 5 3 5 2 6 4
5 6 0 0

8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0

3 8 6 8 6 4
5 3 5 6 5 2 0 0

-1 -1

OUTPUT
1
1
0

并查集int main(){    for (int i=1;i<=10010;i++)        fa[i]=i;    while (1){        int sum=0;        int maxn=0;        int x,y;        cin>>x>>y;        if (x==-1&&y==-1) break;        maxn=max(maxn,x);        maxn=max(maxn,y);        memset(book,0,sizeof(book));              //初始        init(10010);        book[x]=1;        book[y]=1;        while (1){            if (x==0&&y==0) break;            int t1=find(x);            int t2=find(y);            if (t1==t2){                sum=1;            }            if (t1!=t2) fa[t1]=t2;            scanf("%d %d",&x,&y);            maxn=max(maxn,x);            maxn=max(maxn,y);            book[x]=1;            book[y]=1;                                                      }                                                           if (sum==1) cout<<"0"<<endl;                           //多条路径直接输0        else{            sum=0;        for (int i=1;i<=maxn;i++)            if (book[i]==1&&fa[i]==i) sum++;            if (sum==1) cout<<"1"<<endl;            else cout<<"0"<<endl                             //小心,此处要全部联通方可;    }}}//核代