zoukankan      html  css  js  c++  java
  • UVa 10158 War

    Problem B: War

        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

    一道并查集问题,于POJ1182属于同一类型,但更简单一些。

    用relation[x]表示在并查集中,x结点与根结点的关系,relation[x]=0表示x与根节点是朋友关系,relation[x]=1表示x与根节点是敌人关系,并查集查找和合并操作时的关系转换关系

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #define MAX_N 10050
     5 
     6 using namespace std;
     7 
     8 int par[MAX_N],relation[MAX_N];
     9 
    10 void init(int n)
    11 {
    12     memset(relation,0,sizeof(relation));
    13     for(int i=0;i<n;i++)
    14         par[i]=i;
    15 }
    16 
    17 int Find(int x)
    18 {
    19     if(par[x]==x)
    20         return x;
    21     int pre_father=par[x];
    22     par[x]=Find(par[x]);
    23     relation[x]=(relation[x]+relation[pre_father]+2)%2;
    24     return par[x];
    25 }
    26 
    27 void Unite(int a,int b,int x,int y,int r)
    28 {
    29     par[a]=b;
    30     relation[a]=(2+relation[y]+relation[x]+r)%2;
    31 }
    32 
    33 int main()
    34 {
    35     int n;
    36 
    37     while(scanf("%d",&n)==1)
    38     {
    39         int c,x,y;
    40 
    41         init(n);
    42 
    43         while(scanf("%d %d %d",&c,&x,&y)==3&&(c||x||y))
    44         {
    45             if(c==1)
    46             {
    47                 if(Find(x)!=Find(y))
    48                     Unite(Find(x),Find(y),x,y,0);
    49                 else if((relation[x]-relation[y]+2)%2!=0)
    50                     puts("-1");
    51             }
    52             else if(c==2)
    53             {
    54                 if(Find(x)!=Find(y))
    55                     Unite(Find(x),Find(y),x,y,1);
    56                 else if((relation[x]-relation[y]+2)%2!=1)
    57                     puts("-1");
    58             }
    59             else if(c==3)
    60             {
    61                 if(Find(x)==Find(y)&&(relation[x]-relation[y]+2)%2==0)
    62                     puts("1");
    63                 else
    64                     puts("0");
    65             }
    66             else if(c==4)
    67             {
    68                 if(Find(x)==Find(y)&&(relation[x]-relation[y]+2)%2==1)
    69                     puts("1");
    70                 else
    71                     puts("0");
    72             }
    73         }
    74     }
    75 
    76     return 0;
    77 }
    [C++]
  • 相关阅读:
    [开荒啦]ECS服务器初体验
    [邻接矩阵形式]无向图的建立与深度,广度遍历
    [Java 学习笔记] 泛型
    2021辽宁省大学生程序设计竞赛 C D E F G I L
    [DOJ 练习] (取余优化) 判断一个字符串不区分大小写是否回文
    [Acwing Linux基础课] Docker基本操作
    [图解] 数组模拟Trie树
    http://bbs.windows7en.com/thread3102611.html win7
    简单介绍Linux下安装Tomcat的步骤
    linux下导入导出MySQL数据库
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3556609.html
Copyright © 2011-2022 走看看