39 种类并查集

H - Por Costel and the Match

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

原本打算退队,,,,回家了几天与回来了,在宿舍写,退也好不退也好,自己多学点知识,结果并不重要,过程最重要

题目的意思是很简单有n个人分别分属在两个阵营里面,如果两个人在一个阵营里面的话,这两个人是不可以相互射击的,

给你m条信息让你来判断前面给出的提示信息,来判断当前的这条信息是否是正确的;

种类并查集,普通的并查集我们就是把两个点通过变将他们放到一个块内,这一道题目,让我们不光是判断这两个点是不是在一个阵营中,并且如果这两个点不是一个阵营的话我们就要,把这两个点放到两个阵营中去,采用并查集将点放到不同的集合中,

我们用的是添加边,但是这并不是将这两个点放到一个阵营中二是两个阵营中,所以这个是时候我们要加两条边,但是点只有两个该怎么办,如果把这两个点直接加上边的话,就等于是将他们放到一个阵营中(错误),(加边的两个必定是同伙)所以我们在对方的阵营里面找了个特务(方便理解)如果是最初的时候判断a = 1, b = 2 这两个点;

阵营         一                                     二

                1(a*2-1)                      4(b*2)  (加入的边)

                2  (2*a)                            3(b*2-1)(加入的边)

 我们利用a构造了a*2这个点和(特务a*2-1与阵营二同伙)利用b构造了b*2和(b*2-1特务阵营一的同伙);

所以自然而然2和3相连,1和4相连

所以我们在判断的时候只需要判断1和3并且2和4是不是同伙就可以判断当前的这一句话是不是正确的如果是说明不能分成两个阵营(同伙此话错)否则就是正确的我们要吧他们分到不同的阵营中去;

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Max = 2e6+10;
int boss[Max];
int findboss(int a){
  if(a!=boss[a]) boss[a]=findboss(boss[a]);
  return boss[a];
}
void add(int a, int b){
  int aa=findboss(a);
  int bb=findboss(b);
  if(aa>bb) boss[aa]=bb;
  else boss[bb]=aa;
}
int main(){
    freopen("meciul.in","r",stdin);
    freopen("meciul.out","w",stdout);
   int t;
   scanf("%d",&t);
   while(t--){
      int n,m;
      scanf("%d %d",&n,&m);
      for(int i=1;i<=n*2;i++) boss[i]=i;
      for(int i=1;i<=m;i++){
        int a,b;
        scanf("%d %d",&a,&b);
        if(findboss(a*2)==findboss(b*2)&&findboss(a*2-1)==findboss(b*2-1)) {
            printf("NO\n");
        }
        else {
            add(a*2,b*2-1);
            add(a*2-1,b*2);
            printf("YES\n");
        }
      }
   }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39792342/article/details/82945372
39
今日推荐