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;
}

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

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

    裸题. 看之前的模版讲解吧,这里不再赘述了. #include<cstdio> #include<cstring> using namespace std; int fa[10 ...

  2. 【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 ha ...

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

    semipal.in / semipal.out Por Costel the pig, our programmer in-training, has recently returned from ...

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

    semipal.in / semipal.out Por Costel the pig, our programmer in-training, has recently returned from ...

  5. 【数形结合】Gym - 100923I - Por Costel and the Pairs

    perechi3.in / perechi3.out We don't know how Por Costel the pig arrived at FMI's dance party. All we ...

  6. 【动态规划】Gym - 100923A - Por Costel and Azerah

    azerah.in / azerah.out Por Costel the Pig has received a royal invitation to the palace of the Egg-E ...

  7. Gym100923H Por Costel and the Match

    题目链接:http://codeforces.com/gym/100923/problem/H 分析:并查集,用enemy储存x的敌人,用weight储存权重决定根节点 需用scanf和puts输入输 ...

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

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

  9. Codeforces Gym 100463E Spies 并查集

    Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...

随机推荐

  1. at用法小记

    By francis_hao    Aug 22,2017   at – 设置稍后执行的作业. 概要 at [-V] [-f file] [-mMlv] timespec...at [-V] [-f ...

  2. 解决IIS设置多个工作进程中Session失效的问题

    利用StateServer实现Session共享 session保存在专门的StateServer中,该种方式,性能损失比sql略好.比inproc据说有10%-15%的性能损失.怎么使用StateS ...

  3. [hdu 2586]lca模板题(在线+离线两种版本)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 在线版本: 在线方法的思路很简单,就是倍增.一遍dfs得到每个节点的父亲,以及每个点的深度.然后 ...

  4. Maven 标准目录结构

    Maven 标准目录结构 好的目录结构可以使开发人员更容易理解项目,为以后的维护工作也打下良好的基础.Maven2根据业界公认的最佳目录结构,为开发者提供了缺省的标准目录模板.Maven2的标准目录结 ...

  5. java基础学习(一)hashcode

    hashcode的作用 hashCode()方法是从Object类继承过来的,Object类中的hashCode()方法返回的是对象在内存中地址转换成的int值,如果对象没有重写hashCode()方 ...

  6. 【BZOJ1468】Tree [点分治]

    Tree Time Limit: 10 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description 给你一棵TREE,以及这棵树上边的距 ...

  7. hdu 2817 A sequence of numbers(快速幂取余)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817 题目大意:给出三个数,来判断是等差还是等比数列,再输入一个n,来计算第n个数的值. #inclu ...

  8. Python小程序之购物车

    需求: 用户入口: 1.商品信息放在文件中,从文件中读取 2.已购商品,余额记录,第一要输入起始金额,以后不需要二次输入 商家入口: 2.可以添加商品,修改商品价格 # Author:Lee Siri ...

  9. kuangbin带你飞 并查集 题解

    做这套题之前一直以为并查集是很简单的数据结构. 做了才发现自己理解太不深刻.只看重片面的合并集合.. 重要的时发现每个集合的点与这个根的关系,这个关系可以做太多事情了. 题解: POJ 2236 Wi ...

  10. pm2笔记

    概述 pm2是一个进程管理工具.使用pm2部署NodeJS服务可以轻松实现负载均衡. 指定用户启动 pm2启动时会指定一个PM2_HOME目录,作为存放日志文件.rpc.sock文件,默认情况下会PM ...