裸题。

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

#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. ASP.NET 设置DropDownList的当前选项

    1.通过显示字符Text DropDownList.Items.FindByText("你的值").Selected=true; 如果在设置之前,进行过设置,应用如下格式: Dro ...

  2. 【HNOI】d 最小割

    [题目大意]给定一个n*m的土地,每块可以种a或b作物,每种作物在不同的位置有不同的收成,同时,有q个子矩阵中,全部种指定的作物(a或b)会有一定的加成收成,求最大收成. [数据范围] 50% n,m ...

  3. 静态资源(JS/CSS)存储在localStorage

    一.简单了解SEO SEO由英文Search Engine Optimization缩写而来, 中文意译为“搜索引擎优化”.SEO是指从自然搜索结果获得网站流量的技术和过程. 搜索引擎不优化的网站分为 ...

  4. static class 和 non static class 的区别

    static class non static class 1.用static修饰的是内部类,此时这个 内部类变为静态内部类:对测试有用: 2.内部静态类不需要有指向外部类的引用: 3.静态类只能访问 ...

  5. 运维开发:python websocket网页实时显示远程服务器日志信息

    功能:用websocket技术,在运维工具的浏览器上实时显示远程服务器上的日志信息 一般我们在运维工具部署环境的时候,需要实时展现部署过程中的信息,或者在浏览器中实时显示程序日志给开发人员看.你还在用 ...

  6. [Leetcode Week13]Palindrome Partitioning

    Palindrome Partitioning 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/palindrome-partitioning/desc ...

  7. C后端设计开发 - 第2章-内功-数据结构上卷

    正文 第2章-内功-数据结构上卷 后记 如果有错误, 欢迎指正. 有好的补充, 和疑问欢迎交流, 一块提高. 在此谢谢大家了.

  8. [bugfix]copy属性参数将NSMutableArray变为NSArray类型

    问题:NSMutableArray 声明为 copy 属性参数后即使接受NSMutableArray变量依然为NSArray变量 测试: 属性申明为: 1 @property (nonatomic, ...

  9. Spring Boot with Docker

    翻译自:https://spring.io/guides/gs/spring-boot-docker/ Spring Boot with Docker 这篇教程带你一步步构建一个Docker镜像用来运 ...

  10. Java中的原子操作类

    转载: <ava并发编程的艺术>第7章 当程序更新一个变量时,如果多线程同时更新这个变量,可能得到期望之外的值,比如变量i=1,A线程更新i+1,B线程也更新i+1,经过两个线程操作之后可 ...