裸题。

看之前的模版讲解吧,这里不再赘述了。

#include<cstdio>
#include<cstring>
using namespace std;
int fa[100010];
bool rel[100010];
int n,m,T;
int findroot(int x){
if(x==fa[x]){
return x;
}
int Fa=findroot(fa[x]);
if(rel[x]==0 && rel[fa[x]]==0){
rel[x]=0;
}
else if(rel[x]==0 && rel[fa[x]]==1){
rel[x]=1;
}
else if(rel[x]==1 && rel[fa[x]]==0){
rel[x]=1;
}
else if(rel[x]==1 && rel[fa[x]]==1){
rel[x]=0;
}
return fa[x]=Fa;
}
int main(){
// freopen("meciul.in","r",stdin);
// freopen("meciul.out","w",stdout);
int x,y;
scanf("%d",&T);
for(;T;--T){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i){
fa[i]=i;
}
memset(rel,0,sizeof(rel));
for(int i=1;i<=m;++i){
scanf("%d%d",&x,&y);
int f1=findroot(x),f2=findroot(y);
if(f1==f2){
if(rel[x]==rel[y]){
puts("NO");
}
else{
puts("YES");
}
}
else{
fa[f1]=f2;
if(rel[x]==0 && rel[y]==0){
rel[f1]=1;
}
else if(rel[x]==0 && rel[y]==1){
rel[f1]=0;
}
else if(rel[x]==1 && rel[y]==0){
rel[f1]=0;
}
else if(rel[x]==1 && rel[y]==1){
rel[f1]=1;
}
puts("YES");
}
}
}
return 0;
}

【带权并查集】Gym - 100923H - Por Costel and the Match的更多相关文章

  1. 【并查集】Gym - 100923H - Por Costel and the Match

    meciul.in / meciul.out Oberyn Martell and Gregor Clegane are dueling in a trial by combat. The fight ...

  2. Gym-100923H-Por Costel and the Match(带权并查集)

    链接: https://vjudge.net/problem/Gym-100923H 题意: Oberyn Martell and Gregor Clegane are dueling in a tr ...

  3. POJ 1703 Find them, Catch them(带权并查集)

    传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accep ...

  4. [NOIP摸你赛]Hzwer的陨石(带权并查集)

    题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...

  5. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  6. poj1984 带权并查集(向量处理)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5939   Accepted: 2 ...

  7. 【BZOJ-4690】Never Wait For Weights 带权并查集

    4690: Never Wait for Weights Time Limit: 15 Sec  Memory Limit: 256 MBSubmit: 88  Solved: 41[Submit][ ...

  8. hdu3038(带权并查集)

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...

  9. 洛谷OJ P1196 银河英雄传说(带权并查集)

    题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山 ...

随机推荐

  1. JAVA 非对称加密算法RSA

    非对称加密算法 RSA过程 : 以甲乙双方为例 1.初始化密钥 构建密钥对,生成公钥.私钥保存到keymap中 KeyPairGenerator ---> KeyPair --> RSAP ...

  2. STM32-内存管理

    转载:http://www.cnblogs.com/guozhikai/p/6031904.html #ifndef __MALLOC_H #define __MALLOC_H #include &q ...

  3. JDBC原生数据库连接

    我们在开发JavaWeb项目时,常会需要连接数据库.我们以MySQL数据库为例,IDE工具为eclipse,讲述数据库连接与基本操作. 第一步,我们在Web项目的WebContent中建一个简单的前端 ...

  4. FineReport——权限分配以及自定义首页

    权限分配可以有两种方法,第一种方法是根据部门职位分配权限,第二种是根据角色分配权限: FR自带有三个JQ对象,用以保存用户名参数/角色参数/部门参数——$fr_username/$fr_authori ...

  5. 无锁的对象引用:AtomicReference

    http://www.dewen.net.cn/q/9588 首先volatile是java中关键字用于修饰变量,AtomicReference是并发包java.util.concurrent.ato ...

  6. linux服务器上如何显示工作路径

    1. 修改PS环境变量 [root@linux-node01 ~]# vi /etc/bashrc [ "$PS1" = "\\s-\\v\\\$ " ] &a ...

  7. Python+Selenium 自动化实现实例-Xpath捕捉元素的几种方法

    #coding=utf-8import timefrom selenium import webdriverdriver = webdriver.Chrome()driver.get("ht ...

  8. Python datetime描述

    (1)字符串转datetime:>>> string = '2014-01-08 11:59:58'>>> time1 = datetime.datetime.st ...

  9. gitlab和github的区别

          先说一下相同点,二者都是基于web的Git仓库,在很大程度上GitLab是仿照GitHub来做的,它们都提供了分享开源项目的平台,为开发团队提供了存储.分享.发布和合作开发项目的中心化云存 ...

  10. IO扩展控件(System.IO.Abstractions)

    刚看到这个Namespace的时候还以为是.Net Framework里自带的包,结果查了一圈无任何结果.果断上Github搜索,一击即中 https://github.com/tathamoddie ...