首页 > 其他分享 >[AcWing 258] 石头剪刀布

[AcWing 258] 石头剪刀布

时间:2022-08-16 21:23:32浏览次数:63  
标签:return 裁判 int same merge 258 剪刀 find AcWing

image
image
image

带扩展域的并查集


点击查看代码
#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 1e6 + 10;

int n, m;
int p[N];

struct Node
{
    int a, b;
    char op;
} s[N];

int find(int x)
{
    if (p[x] != x)
        p[x] = find(p[x]);
    return p[x];
}

void merge(int a, int b)
{
    int pa = find(a), pb = find(b);
    p[pa] = pb;
}

bool same(int a, int b)
{
    return find(a) == find(b);
}

bool check(int a, int b, char c)
{
    if (c == '=') {
        if (same(a, b + n) || same(a + n, b))
            return false;
        merge(a, b);
        merge(a + n, b + n);
        merge(a + n + n, b + n + n);
    }
    else if (c == '>') {
        if (same(a, b) || same(a + n, b))
            return false;
        merge(a, b + n);
        merge(a + n, b + n + n);
        merge(a + n + n, b);
    }
    else {
        if (same(a, b) || same(a, b + n))
            return false;
        merge(a + n, b);
        merge(a + n + n, b + n);
        merge(a, b + n + n);
    }
    return true;
}

void solve()
{
    while (cin >> n >> m) {
        for (int i = 1; i <= m; i ++) {
            int a, b;
            char op;
            cin >> a >> op >> b;
            s[i] = {a, b, op};
        }
        // 记录裁判的个数
        int cnt = 0;
        // 记录裁判的编号
        int res = 0;
        // 记录得到裁判信息的行号
        int the_line = 0;
        // 假设编号为 t 的人是裁判
        for (int t = 0; t < n; t ++) {
            // 每次都要将集合初始化
            for (int i = 0; i < 3 * n; i ++)
                p[i] = i;
            bool is_ok = true;
            for (int i = 1; i <= m; i ++) {
                auto& [a, b, op] = s[i];
                // 将裁判跳过
                if (a == t || b == t)
                    continue;
                // 检查是否合法,合法则合并集合,不会进入if中
                if (!check(a, b, op)) {
                    // 不合法才会进入
                    is_ok = false;
                    // 出现问题的那一行
                    if (the_line <= i)
                        the_line = i;
                    break;
                }
            }
            if (is_ok) {
                res = t;
                cnt ++;
            }
        }
        if (!cnt)
            printf("Impossible\n");
        else if (cnt > 1)
            printf("Can not determine\n");
        else
            printf("Player %d can be determined to be the judge after %d lines\n", res, the_line);
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}

  1. 判断裁判的思路
    先假设一定存在裁判,枚举把每个人当作裁判,然后将包含他的数据跳过,判断其他数据是否会推出矛盾,如果没有矛盾,则说明这个人可能是裁判,就把 \(cnt + 1\),考虑以下几种情况:
    ① 如果没有裁判,
    ② 如果只有一位裁判,那么只有当枚举到把他当作裁判时,其他数据才会没有矛盾,\(cnt + 1\) 只会执行一次,最终 \(cnt = 1\)
    ③ 如果有裁判,但是不能确定哪个是裁判,那么有多个人选,把他当作裁判时,其他数据没有矛盾,\(cnt + 1\) 会执行超过一次,\(cnt > 1\)
  2. 扩展域
    对于 \(a\) 号小朋友,\(a\) 代表该小朋友出的是石头,\(a + n\) 代表该小朋友出的是剪刀,\(a + n + n\) 代表该小朋友出的是布
  3. 最早的找到裁判的时间 \(\Leftrightarrow\) 判断出除他以外的人都不是裁判的时间 \(\Leftrightarrow\) 判断出其他人不是裁判的时间的最大值

标签:return,裁判,int,same,merge,258,剪刀,find,AcWing
From: https://www.cnblogs.com/wKingYu/p/16593028.html

相关文章

  • AtCoder Beginner Contest 258
    A-When?问21:00后的第k分钟的时间#include<bits/stdc++.h>usingnamespacestd;constintN=2e5+5;intn,a[N],cnt,k;int32_tmain(){ intn,h=21......
  • [AcWing 145] 超市
    贪心+小根堆点击查看代码#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;typedefpair<int,int>PII;constintN=1e6+10;intn;......
  • AcWing周赛62-64 中比较有意思的小题题解
    AcWing周赛62-64(选讲)感觉比较思维4502.集合操作https://www.acwing.com/problem/content/4505/根据题意,肯定要使得所取的最大值最大,平均值最小。又因为每次放进来的的......
  • [AcWing 4507] 子数组异或和
    异或的性质点击查看代码#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;constintN=1e6+10;intn;inta[N];voidsolve(){......
  • AcWing3293.统计单词
    原题链接string.back()、string.pop_back()妙用#include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;intmain(){stringstr;......
  • Acwing 第 64 场周赛 C 4507. 子数组异或和(异或+前缀和)
    https://www.acwing.com/problem/content/4510/给定一个长度为n的整数数组a1,a2,…,an。请你统计一共有多少个数组a的非空连续子数组能够同时满足以下所有条件:该......
  • AcWing3391.今年第几天?(日期题)
    原题链接https://www.acwing.com/problem/content/3394/日期题思路满足下面条件之一的是闰年:年份是4的整数倍,而且不是100的整数倍;年份是400的整数倍。处理输......
  • acwing 1228. 油漆面积 扫描线
     X星球的一批考古机器人正在一片废墟上考古。该区域的地面坚硬如石、平整如镜。管理人员为方便,建立了标准的直角坐标系。每个机器人都各有特长、身怀绝技。它们感兴......