【并查集】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 is extremely important, as the life of Tyrion Lannister is on the line. Oberyn and Gregor are measuring their skill in combat the only way the two best fighters in Westeros can, a match of Starcraft. The one who supervises the match in none other than Por Costel the pig.

Oberyn and Gregor are both playing the Terrans, and they confront each other in the middle of the map, each with an army of Marines. Unfortunately, pigs cannot distinguish colors that well, that is why Por Costel can‘t figure out which marine belongs to which player. All he sees is  marines in the middle of the map and, from time to time, two marines shooting each other. Moreover, it might be the case that Por Costel‘s imagination will play tricks on him and he will sometimes think two marines are shooting each other even though they are not.

People are starting to question whether Por Costel is the right person for this important job. It is our mission to remove those doubts. You will be given Por Costel‘s  observations. An observation consists in the fact that Por Costel sees that marine  and marine  are shooting each other. We know that marines in the same team (Oberyn‘s or Gregor‘s) can never shoot each other. Your task is to give a verdict for each observation, saying if it is right or not.

An observation of Por Costel‘s is considered correct if, considering this observation true and considering all the correct observations up to this point true, there is a way to split the marines in "Oberyn‘s team" and "Gregor‘s team" such that no two marines from the same team have ever shot each other. Otherwise, the observation is considered incorrect.

"Elia Martell!!! You rushed her! You cheesed her! You killed her SCVs!"

Input

The input file meciul.in will contain, on its first line, the number of tests  (). A test has the following structure: the first line contains integers  () and  () and the next  lines each contain a pair of integers  and  () describing an observation of Por Costel‘s.

Output

The output file meciul.out will contain one line for each of Por Costel‘s observations, on each test. The line will contain "YES" if the observation is correct and "NO" otherwise. It is not necessary to leave extra spacing between the outputs of different test cases.

Example

Input

13 31 22 31 3

Output

YESYESNO

给你一些信息(x,y),告诉你x和y是敌人,让你根据某条信息之前的合法信息,判断当前信息是否合法。

并查集。记录x的敌人集合为enemy[x],只需要存一个代表元即可。

然后如果某条信息合法的话,就将x和enemy[y]合并,y和enemy[x]合并即可。

很像当年入门的时候一道并查集经典题“团伙”。

#include<cstdio>
#include<cstring>
using namespace std;
int fa[100010],__rank[100010],enemy[100010];
int T,n,m;
int findroot(int x)
{
	return x==fa[x] ? x : fa[x]=findroot(fa[x]);
}
int Union(int U,int V)
{
	if(__rank[U]<__rank[V])
	  fa[U]=V;
	else
	  {
	  	fa[V]=U;
	  	if(__rank[U]==__rank[V])
	  	  ++__rank[U];
	  }
}
int main()
{
	//freopen("h.in","r",stdin);
	freopen("meciul.in","r",stdin);
	freopen("meciul.out","w",stdout);
	int x,y;
	scanf("%d",&T);
	for(;T;--T)
	  {
	  	memset(__rank,0,sizeof(__rank));
	  	memset(enemy,0,sizeof(enemy));
	  	scanf("%d%d",&n,&m);
	  	for(int i=1;i<=n;++i)
	  	  fa[i]=i;
	  	for(int i=1;i<=m;++i)
	  	  {
	  	  	scanf("%d%d",&x,&y);
	  	  	int f1=findroot(x),f2=findroot(y);
	  	  	if(f1==f2)
	  	  	  puts("NO");
	  	  	else
	  	  	  {
	  	  	  	if(!enemy[x])
	  	  	  	  enemy[x]=y;
	  	  	  	else
	  	  	  	  Union(f2,findroot(enemy[x]));
	  	  	  	if(!enemy[y])
	  	  	  	  enemy[y]=x;
	  	  	  	else
	  	  	  	  Union(f1,findroot(enemy[y]));
	  	  	  	puts("YES");
	  	  	  }
	  	  }
	  }
	return 0;
}
时间: 2024-10-11 06:32:21

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

Por Costel and the Match Gym - 100923H(经典种类并查集)

Por Costel and the Match Gym - 100923H 题目链接:https://vjudge.net/problem/Gym-100923H 题目: Oberyn Martell and Gregor Clegane are dueling in a trial by combat. The fight is extremely important, as the life of Tyrion Lannister is on the line. Oberyn and Gr

【Heap-dijkstra】Gym - 100923B - Por Costel and the Algorithm

algoritm.in / algoritm.out Even though he isn't a student of computer science, Por Costel the pig has started to study Graph Theory. Today he's learning about Bellman-Ford, an algorithm that calculates the minimum cost path from a source node (for in

【分块打表】Gym - 100923K - Por Costel and the Firecracker

semipal.in / semipal.out Por Costel the pig, our programmer in-training, has recently returned from the Petrozaporksk training camp. There, he learned a lot of things: how to boil a cob, how to scratch his belly using his keyboard, etc... He almost r

【找规律】Gym - 100923L - Por Costel and the Semipalindromes

semipal.in / semipal.out Por Costel the pig, our programmer in-training, has recently returned from the Petrozaporksk training camp. There, he learned a lot of things: how to boil a cob, how to scratch his belly using his keyboard, etc... He almost r

Codeforces Gym 100463E Spies 并查集

Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Description In the aftermath of Canada’s annexation of Pittsburgh tensions have been pretty high between Canada and the US. You have personally been hired

GYM 101173 F.Free Figurines(贪心||并查集)

原题链接 题意:俄罗斯套娃,给出一个初始状态和终止状态,问至少需要多少步操作才能实现状态转化 贪心做法如果完全拆掉再重装,答案是p[i]和q[i]中不为0的值的个数.现在要求寻找最小步数,显然要减去一些多余的步数.如果初始的一些链的前端是终止的某一条链的连续的一部分,那么这条链就不用被拆开再连上,这样每一个长度为x的链对答案的贡献就是-2*(x-1),对每条链进行同样的操作之后就是答案 #include <iostream> #include<cstdio> #include<

【并查集】Gym - 101128B - Black Vienna

有26张牌(A~Z),其中三张被拿走了.其余23张被分发给了两个人.给你m次调查结果,一次调查结果是对其中一个人询问一对牌,他会告诉你他有这对牌的几张(0~2).问你有多少种被拿走的牌的组合. 三重循环枚举被拿走的牌. 然后对于一次调查,我们发现可能的十二种情况中({这两张牌都被拿走,都不被,其中一张被拿走,其中另一张被拿走} × {回答:0,1,2}),只有一种情况我们不能确定它们的归属,或者无解.即两张牌都不被拿走,并且回答是1.那么必然其中一张牌在一个人手上,另一张牌在另一人手上,但我们不

Codeforces Gym 101194G Pandaria (2016 ACM-ICPC EC-Final G题, 并查集 + 线段树合并)

题目链接  2016 ACM-ICPC EC-Final Problem G 题意  给定一个无向图.每个点有一种颜色. 现在给定$q$个询问,每次询问$x$和$w$,求所有能通过边权值不超过w的边走到$x$的点的集合中,哪一种颜色的点出现的次数最多. 次数相同时输出编号最小的那个颜色.强制在线. 求哪种颜色可以用线段树合并搞定. 关键是这个强制在线. 当每次询问的时候,我们先要求出最小生成树在哪个时刻恰好把边权值不超过$w$的边都用并查集合并了. 在做最小生成树的时候每合并两个节点,另外开一个

【Gym - 100923A】Por Costel and Azerah(思维水题)

Por Costel and Azerah Descriptions 给你n个数 问你,有多少个子序列 的和是偶数 Example Input 233 10 124 2 Output 33 题目链接 https://vjudge.net/problem/Gym-100923A 恶心死了   freopen("azerah.in","r",stdin); freopen("azerah.out","w",stdout); 必须加