UVa - 10158 - War ( 并查集 )

来源:互联网 发布:单片机应用系统实例 编辑:程序博客网 时间:2024/05/18 03:21

  A war is being lead between two countries, A and B. As a loyal citizen of C, you decide to help your country’s espionage by attending the peace-talks taking place these days (incognito, of course). There are n people at the talks (not including you), but you do not know which person belongs to which country. You can see people talking to each other, and through observing their behaviour during their occasional one-to-one conversations, you can guess if they are friends or enemies. In fact what your country would need to know is whether certain pairs of people are from the same country, or they are enemies. You may receive such questions from C’s government even during the peace-talks, and you have to give replies on the basis of your observations so far. Fortunately nobody talks to you, as nobody pays attention to your humble appearance.

 

  Abstract

    Now, more formally, consider a black box with the following operations:

               setFriends(x, y)     shows that x and y are from the same country

               setEnemies(x, y)   shows that x and y are from different countries

               areFriends(x, y)     returns true if you are sure that x and y are friends

               areEnemies(x, y)   returns true if you are sure that x and y are enemies

    The first two operations should signal an error if they contradict with your former knowledge. The two relations ‘friends’ (denoted by ~) and ‘enemies’ (denoted by *) have the following properties:

              ~ is an equivalence relation, i.e.

1.      If x ~ y and y ~ z then x ~ z  (The friends of my friends are my friends as well.)

2.      If x ~ y then y ~ x                  (Friendship is mutual.)

3.      x ~ x                                       (Everyone is a friend of himself.)

              * is symmetric and irreflexive

4.      If x * y then y * x                  (Hatred is mutual.)

5.      Not x * x                                (Nobody is an enemy of himself.)

              Also

6.      If x * y and y * z then x ~ z   (A common enemy makes two people friends.)

7.      If x ~ y and y * z then x * z   (An enemy of a friend is an enemy.)

    Operations setFriends(x, y) and setEnemies(x, y) must preserve these properties.

 

 

  Input

     The first line contains a single integer, n, the number of people.

     Each of the following lines contains a triple of integers, c x y, where c is the code of the operation:

            c = 1, setFriends

            c = 2, setEnemies

            c = 3, areFriends

            c = 4, areEnemies

     and x and y are its parameters, which are integers in the range [0, n), identifying two (different) people. The last line contains 0 0 0.

    All integers in the input file are separated by at least one space or line break.

 

  Output

     For every ‘areFriends’ and ‘areEnemies’ operation write 0 (meaning no) or 1 (meaning yes) to the output. Also for every ‘setFriends’ or ‘setEnemies’ operation which contradicts with previous knowledge, output a –1 to the output ; note that such an operation should produce no other effect and execution should continue. A successful ‘setFriends’ or ‘setEnemies’ gives no output.

    All integers in the output file must be separated by at least one space or line break.

 

  Constraints

    n < 10000, the number of operations is unconstrained.

 

 

  Sample Input

            10

            1 0 1

            1 1 2

            2 0 5

            3 0 2

            3 8 9

            4 1 5

            4 1 2

            4 8 9

            1 8 9

            1 5 2

            3 5 2

            0 0 0

 

  Sample Output

            1

            0

            1

            0

            0

            -1

            0


题目大意

给定数字 n 表示 n 个人,接着输入c ,a ,b 三个数字,c 的 值可以是 1 2 3 4 分别代表 :建立友谊关系,建立敌对关系,查询是否是朋友,查询是否敌对。

a和b分别代表编号为a的人,编号为b的人。

建立友谊关系,如果有矛盾。即已经是敌对关系,那么输出-1  否则不输出

建立敌对关系,一样

查询是否敌对或者友谊,如果是,输出1,否则0、

这里表示敌对和友谊是用一个数组,前半范围表示友谊关系,后半范围表示敌对关系。

这里给读者留一个问题:可以用2 个数组,分别代表友谊关系和敌对关系吗?( 欢迎评论 )


#include<cstdio>#define N 10001using namespace std;int p[N+N]; //第一个N表示朋友 第二个N表示敌人 int t,c,a,b;int find(int x){return p[x] == x ? x : p[x] = find(p[x]);}void solve(){int a1 = find(a); int a2 = find(a+N);int b1 = find(b); int b2 = find(b+N);if(c==1){//a的朋友是b的敌人 敌对关系 if((a1==b2)) puts("-1");else{p[a1] = b1; //b的朋友成为a的朋友p[a2] = b2; //b的敌人成为a的敌人 }}if(c==2){//a的朋友是b的朋友  朋友关系 if((a1==b1)) puts("-1");else{p[a1] = b2; //b的敌人成为a的朋友 p[a2] = b1;//b的朋友成为a的朋友 }}if(c==3){//a的朋友是b的朋友 朋友关系 if(a1==b1) puts("1");else{puts("0");}}if(c==4){//a的朋友是b的敌人 if(a1==b2) puts("1");else{puts("0");}}}int main(){while(~scanf("%d",&t)) {for(int i=0 ;i< 2*N;i++) {p[i] = i;}while(~(scanf("%d%d%d",&c,&a,&b))&&c){solve();}}return 0;} 





0 0
原创粉丝点击