热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

UVA10158War并查集

D-War(8.4.3)CrawlinginprocessCrawlingfailedTimeLimit:3000MS    MemoryLimit:0KB 
D - War(8.4.3)
Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit Status

Description

技术分享
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

 ACcode:

#include 
#include 
#define maxn 20000+5
using namespace std;
int set[maxn],n,loop;
int set_find(int t){
    if(set[t]<0)
        return t;
    else return set_find(set[t]);
}
int areFriends(int x,int y){
    if(set_find(x)!=set_find(y)&&set_find(x)!=set_find(y+n))return -1;
    if(set_find(x)==set_find(y))return 1;
    return 0;
}
int areEnemies(int x,int y){
    if(set_find(x)==set_find(y+n))return 1;
    else return -1;
}
int setFriends(int x,int y){
    if(areFriends(x,y)==0){
            return -1;
    }
    if(areFriends(x,y)==-1){
        set[set_find(x)]=set_find(y);
        set[set_find(x+n)]=set_find(y+n);
    }
    return 1;
}
int setEnemies(int x,int y){
    if(areFriends(x,y)==1)return -1;
    if(areFriends(x,y)==-1){
        set[set_find(x+n)]=set_find(y);
        set[set_find(x)]=set_find(y+n);
    }
    return 1;
}
int main(){
    scanf("%d",&n);
    for(int i=0;i<2*n+10;++i)set[i]=-1;
    int c,x,y,t;
    while(scanf("%d%d%d",&c,&x,&y)!=EOF&&(c||x||y)){
        if(c==1){
            t=setFriends(x,y);
            if(t==-1)printf("-1\n");
        }
        else if(c==2){
            t=setEnemies(x,y);
            if(t==-1)printf("-1\n");
        }
        else if(c==3){
            t=areFriends(x,y);
            printf(t==1?"1\n":"0\n");
        }else if(c==4){
            t=areEnemies(x,y);
            printf(t==1?"1\n":"0\n");
        }
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

UVA 10158 War 并查集


推荐阅读
  • Forexamplewehavefollowingcode:$(el).hide()el.style.display'none'$(el).forEach((){ ... [详细]
  • JS swiper轮播图完美兼容手机端
    swiper ... [详细]
  • 【7】继承、super、this、抽象类
    1、继承定义:继承就是子类继承父类的属性和行为,使得子类对象具有与父类相同的属性、相同的行为。子类可以直接访问父类中的非私有的属性和行为。好处:1、提高代码的复用性。2、类与类之间 ... [详细]
  • 状压dfs。。。。GemsFight!TimeLimit:2000010000MS(JavaOthers)    MemoryLimit:327680327680K ... [详细]
  • 一、Web前端技术HTML:HTML、HTML5、CSS、TCPIPXML:XMLWeb脚本:JavaScript、AJAX、jQuery、JSONServ脚本:JSP、APS、P ... [详细]
  • 如何绘制直观易懂的时标网络图
    时标网络图是用活动的定位和长度表示活动历时的项目网络图。是含网络逻辑的横道图,并且是任何以工作位置和长度代表其持续时间的项目网络图。项目经理圈子在时标网络图中,以实箭线表示工作,实 ... [详细]
  • 一,深浅拷贝看拷贝列子day19-1.py假如修改的元素是一个列表,源列表也会发生变化day19-2.py为什么会这样,因为第一次修改的是一个不可变元素对应的指针发生了变化,第二次 ... [详细]
  • 实验六提交版
    1.21.3part2共用体与结构体类型的区别?答:共用体与结构体的区别在于它们的表示方法不同。结构体内,结构体的各成员顺序排列存储,每个成员都有自己独立的存储位置,而共用体的情况 ... [详细]
  • TP框架 事件
    原文 http:www.cnblogs.comFushichop6600241.html1.在程序运行到应用模块的时候,先进行事件的注册:对事件进行监听注册监听注册其中,获取监听权 ... [详细]
  • 获取鼠标的位置/坐标
    使用javascript如何获取鼠标的位置呢?获取光标的位置?获取鼠标坐标先看效果?核心方法:****返回鼠标的坐标*@parame*@returns{{x ... [详细]
  • 一直以为,情商很重要,要注意提高自己的情商,注意学习为人处世,“世事洞明皆学问”。时间久了,反而觉得,也许情商并没有想象中的那么重要。有时候,决定一个人的层次,并不是靠情商,而是靠 ... [详细]
  • 注意:用心找自己做的项目中自己感觉最拿出来手的(复杂度最高,用的技术最多的项目),描述的时候尽可能往里面添加一些技术名词布局我们用html5+css3我们会用reset.css重置 ... [详细]
  • PubMed数据下载
    目标站点分析目标:抓取页面中的机构名称,日期,标题,作者,作者信息 ... [详细]
  • 课程简介和学习安排1-1强力django+杀手级xadmin打造上线标准的在线教育平台试看第2章开发环境搭建-linux本章节将会带领大家在windows上通过虚拟机安装linux ... [详细]
  • 分隔超平面:将数据集分割开来的直线叫做分隔超平面。超平面:如果数据集是N维的,那么就需要N-1维的某对象来对数据进行分割。该对象叫做超平面,也就是分类的决策边界。间隔:一个点 ... [详细]
author-avatar
谭禅心_136
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有