并查集的应用 直接阔成2倍。后N项为对应的敌人

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
#define MAXN 10010
int p[MAXN * ];
int Find(int x) {return x == p[x] ? x : p[x] = Find(p[x]);}
int N,M;
int main()
{
//freopen("sample.txt","r",stdin);
while (scanf("%d",&N) != EOF)
{
for (int i = ; i <= N * ; i++) p[i] = i;
int op,x,y;
while (scanf("%d%d%d",&op,&x,&y) != EOF)
{
if (op == && x == && y == ) break;
int u = Find(x), v = Find(y);
int eu = Find(x + N) , ev = Find(y + N);
switch (op)
{
case : if (u == ev) puts("-1");
else
{
p[u] = v;
p[eu] = ev;
}
break;
case : if (u == v) puts("-1");
else
{
p[u] = ev;
p[eu] = v;
}
break;
case : if (u == v) puts("");
else puts("");
break;
case : if (u == ev) puts("");
else puts("");
break;
}
}
}
return ;
}

Uva 10158 War的更多相关文章

  1. UVA 10158 War(并查集)

    //思路详见课本 P 214 页 思路:直接用并查集,set [ k ]  存 k 的朋友所在集合的代表元素,set [ k + n ] 存 k  的敌人 所在集合的代表元素. #include< ...

  2. uva 10158

    并查集 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> #in ...

  3. UVA 12035 War Map

    让我先把微笑送给出题人 这个题最基础的一个想法:先找出一个度数和为总度数和的1/2的点集,然后判断这个点集和这个点集的补集能否形成二分图.但是就算我们把判断的复杂度看成O(1),这个算法的复杂度也是 ...

  4. UVA 10158 并查集的经典应用

    这个题目一看就是用并查集,有N个国家代表,在M行给出两两之间的关系,敌人或者朋友,(当然如果该关系跟已知关系冲突,则输出-1) 关系的几个约束条件时这样的 在朋友方面,朋友的朋友就是自己的朋友,这个就 ...

  5. runtime error 的原因

    1.  数组访问越界 2.  分母为 0 3.   括号 做题时偶然发现的! (详见 UVA 10158 War) 代码中出现了这种东西 else if( arefriends(x,y==-1)  ) ...

  6. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  7. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  8. ACM训练计划step 1 [非原创]

    (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...

  9. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

随机推荐

  1. HyperLedger Fabric 1.4 比特币历史(1.1)

    比特币是一种数字货币,也是一种创新思维,把人们带入到一个无中心化.完全可信任.安全可靠的全新思维领域:一个叫“中本聪”的人或组织,使我们思维产生化学反应,他在2008年10月31日发表了比特币白皮书& ...

  2. CentOS网络设置 couldn't resolve host 'mirrorlist.centos.org问题解决

    在VMWare上安装好centos后,使用yum安装nodejs报错:can not resolve host 'mirritlist.centos.org', 百度上很多都说在/etc/resolv ...

  3. 笔记-git-.gitignore

    笔记-git-.gitignore 1.      git忽略文件 有的文件不需要提交到公共仓库中,为此git提供了三种实现方式. gitignore文件 在项目的设置中指定排除文件 定义全局.git ...

  4. mysql学习第二天函数

    -- 1.绝对值 select abs(-1)from dual -- 2.求平方根select sqrt(6)from dual -- 3.圆周率select pi()from dual -- 4. ...

  5. 基于HDP版本的YDB安装部署(转)

    第三章 YDB依赖环境准备 一.硬件环境 硬件如何搭配,能做到比较高的性价比,不存在短板.合理的硬件搭配,对系统的稳定性也很关键. 1.CPU不是核数越高越好,性价比才是关键. 经常遇到很多的企业级客 ...

  6. CNN:

    (1)卷积:对图像元素的矩阵变换,是提取图像特征的方法,多种卷积核可以提取多种特征.一个卷积核覆盖的原始图像的范围叫做感受野(权值共享).一次卷积运算提取的特征往往是局部的,难以提取出比较全局的特征, ...

  7. Java-JNA使用心得

    自上个月20号,历时整整一个月,终于找到工作入职了. 然后这段时间一直看公司的框架还有业务方面的东西.其实由于给分配了一个研究Java调用C语言接口的问题,导致框架业务方面的东西还不熟,然后现在手上又 ...

  8. Python 3基础教程14-在文件尾部更新内容

    本文介绍在一个已经存在的文件尾部添加内容,还是用到write方法. 这里exampleFile.txt是前面文件创建的文件,里面有两行文字.

  9. python 学习分享-实战篇高级的ftp

    #server代码 import socketserver,os,hashlib Base_paht = os.path.dirname(os.path.dirname(os.path.abspath ...

  10. 小红帽安装centos的yum的一些坑!

    [root@localhost ~]# lsanaconda-ks.cfg yum-3.4.3-158.el7.centos.noarch.rpm yum-updateonboot-1.1.31-45 ...