UVa 10158 War / 并查集

来源:互联网 发布:php开源代码 编辑:程序博客网 时间:2024/04/30 15:42

我是百度的 嗯 并查集已经忘了 现在复习下

#include <cstdio>const int MAX = 20010;int frien[MAX];int find(int x){if(x != frien[x])frien[x] = find(frien[x]);return frien[x];}int main(){int n;int i;int a,b,c;while(scanf("%d",&n)!=EOF){for(i = 0;i < 2*n; i++){frien[i] = i;}while(scanf("%d %d %d",&c,&a,&b),c||a||b){int f1 = find(a);int f2 = find(b);int e1 = find(a+n);int e2 = find(b+n);if(c == 1)//朋友 {if(f1 == e2) //f1集合的敌人是e2集合 那么ab不可能是朋友 输出-1{puts("-1");continue;}frien[f1] = f2;frien[e1] = e2;}else if(c == 2){if(f1 == f2)//ab是朋友 他们不可能是敌人 矛盾{puts("-1");continue;}frien[f1] = e2;frien[e1] = f2;}else if(c == 3){if(f1 == f2)puts("1");elseputs("0");}else{if(f1 == e2)puts("1");elseputs("0");}}}return 0;}/*101 0 11 1 22 0 53 0 23 8 94 1 54 1 24 8 91 8 91 5 23 5 20 0 0*/


 

 

0 0
原创粉丝点击