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
1
3 3
1 2
2 3
1 3
Output
YES
YES
NO

给你一些信息(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;
}

最新文章

  1. httpclient4 文档翻译
  2. android 开发中 添加库文件 和so 文件的存放位置和添加依赖
  3. box-shadow
  4. vc 获取网络时间
  5. soapUI 时间格式
  6. js星级评分点击星级评论打分效果
  7. sitemesh使用步骤
  8. Opencl API解释(二)
  9. ios 开发指南
  10. android可拖动排序GridView实现
  11. linux cmd: ps
  12. mysql 本机root密码忘记
  13. FatMouse&amp;#39; Trade(杭电1009)
  14. 在地铁上看了zabbix 的书发现 &quot;报警执行远程命令&quot;
  15. spy++捕获窗口消息
  16. Laravel 出现 No application encryption key has been specified.
  17. java 实验 三 (1)
  18. 学习笔记TF050:TensorFlow源代码解析
  19. C# 获取两个时间段之间的所有时间与获取当前时间所在的季度开始和结束时间
  20. POJ 3258:River Hopscotch (最大化最小值)

热门文章

  1. ionic3自定义图标
  2. 在Idea中使用Eclipse编译器
  3. bzoj4602 [Sdoi2016]齿轮
  4. COGS727 [网络流24题] 太空飞行计划
  5. 人脸识别 - 环境搭建(Ubuntu 16.04)
  6. 千万不要使用xfce和KDE版Manjaro Linux--之荒谬言论
  7. Shell中的while循环【转】
  8. 取Session数据语句在应放在哪里
  9. centos 7 卸载自带的jdk
  10. JavaScript的字符串详解