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.
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.
1 3 3 1 2 2 3 1 3
YES YES NO
题目大意:给定你n个小兵,不确定小兵属于双方中的哪一方,规定了同一方的小兵不能互相攻击。现在给出m个信息,初始状态下小兵无阵营,每次给出两个小兵的编号,如果两个小兵可能互相攻击,则输出YES,且将这个信息应用到之后的判定中,否则输出NO,且不使用这条信息。
题解:本题可以将每个小兵的状态模拟成两个点,分别记录其属于第一个阵营和第二个阵营,然后使用并查集,对于每个信息先判定两个小兵是不是在同一个阵营(双方都判)如果是,则信息无效,如果不是,则在并查集中加入两条边,依次处理
代码如下
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int fa[200005];
int find1(int a)
{
if(fa[a]!=a)
fa[a]=find1(fa[a]);
return fa[a];
}
void union1(int a,int b)
{
fa[find1(a)]=find1(b);
}
int main()
{
freopen("meciul.in","r",stdin);
freopen("meciul.out","w",stdout);
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
//memset(fa,0,sizeof(fa));
for(int i=1;i<=2*n;i++)
fa[i]=i;
for(int i=0;i<m;i++)
{
int l,r;
scanf("%d %d",&l,&r);
//cout<<l<<r<<endl;
if(find1(2*l)==find1(2*r)&&find1(2*l-1)==find1(2*r-1))
{
// cout<<find1(2*l)<<' '<<find1(2*r)<<endl;
printf("NO\n");
}
else
{
printf("YES\n");
union1(2*l,2*r-1);
union1(2*l-1,2*r);
}
}
}
//cout << "Hello world!" << endl;
return 0;
}