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

时间:2019-08-18
本文章向大家介绍Por Costel and the Match Gym - 100923H(经典种类并查集),主要包括Por Costel and the Match Gym - 100923H(经典种类并查集)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Por Costel and the Match 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 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
题意:每次给你两个数,是敌人情况,输出“YES”;一旦遇到不是敌人情况,输出“NO”;
思路:一开始以为就是单纯的并查集模板题,后来想到之前做过一题它有多种情况,所以类似这个,比如1,2,3,4,共有4个人,1和4
是敌人关系,那么这时我们再引出四个人,一共有8个人,12345678,1和4是敌人关系,那么5和8也是敌人关系,那么1和5就是友人关系,
4和8就是友人关系,1和2再是敌人关系,那么5和6就是敌人关系,这时候1和5还是友好关系,2和6就是友好关系,这样关系就不会出现矛盾的情况
 
//
// Created by HJYL on 2019/8/15.
//
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
int father[maxn];
int p[maxn];
struct Node{
    int x,y;
}node[maxn];
int find(int x)
{
    if(x==father[x])
        return x;
    return father[x]=find(father[x]);
}
void merge(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
        father[fx]=fy;
}
int main()
{
    //freopen("C:\\Users\\asus567767\\CLionProjects\\untitled\\text","r",stdin);
    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<<1);i++)
            father[i]=i;
        int x,y;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            int xx=find(x);
            int yy=find(y);
            if(xx==yy)
                printf("NO\n");
            else{
                merge(x,y+n);
                merge(x+n,y);
                printf("YES\n");
            }
        }
    }
    return 0;
}



原文地址:https://www.cnblogs.com/Vampire6/p/11373386.html