ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-08-18 15:57:28  阅读:306  来源: 互联网

标签:Level int Gym 查集 Por each include Costel


链接:

https://vjudge.net/problem/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!"

思路:

带权并查集, 维护每个节点与根节点的关系,为0是友军,为1是敌军,

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;

const int MAXN = 1e5+10;
int Fa[MAXN], Level[MAXN];
int n, m;

int GetF(int x)
{
    if (Fa[x] == x)
        return x;
    int tmp = Fa[x];
    Fa[x] = GetF(Fa[x]);
    Level[x] = Level[x]^Level[tmp];
    return Fa[x];
}
//1 1 3
//0 1 0
//1 2 3
//0 same 1 diff
int main()
{
//    freopen("meciul.in", "r", stdin);
//    freopen("meciul.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        for (int i = 1;i <= n;i++)
            Fa[i] = i, Level[i] = 0;
        int l, r;
        while (m--)
        {
            cin >> l >> r;
            int tl = GetF(l);
            int tr = GetF(r);
            if (tl != tr)
            {
                Level[tr] = 1^Level[l]^Level[r];
                Fa[tr] = tl;
                cout << "YES" << endl;
            }
            else
            {
                if (Level[l] == Level[r])
                    cout << "NO" << endl;
                else
                    cout << "YES" << endl;
            }
//            for (int i = 1;i <= n;i++)
//                cout << Level[i] << ' ' ;
//            cout << endl;
        }
    }

    return 0;
}

标签:Level,int,Gym,查集,Por,each,include,Costel
来源: https://www.cnblogs.com/YDDDD/p/11372646.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有