poj - 2912 Enumeration + kind and lookup

The meaning of the question: There are many people playing rock-paper-scissors, and each person chooses which gesture to make in advance, and there is a special person who can make any gesture at will. Ask whether he can judge from a series of rock-paper-scissors games given. Which one is special can be judged from the number of games.

Define 0, 1, and 2 as three kinds of gestures, enumerate each person, do not perform the union search operation for this person, and see if there will be contradictions in the rest, if not, it may be. The number of games is to enumerate the maximum number of games in which everyone else has a conflict, because only by negating everyone else can you determine that this person is special.

Pay attention to entering that character. . . . . .

Link: poj 2912

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <vector>
#include <sstream>
#define ll long long

using namespace std;

const int maxn = 2000 + 10;
const int maxm = 1000000 + 10;
const int inf = 0x3f3f3f3f;

int fa[maxn];
int r[maxn];
int error[maxn];

int n, m, k;

struct node {
    int a, b, c;
}q[maxn];

void init() {
    for(int i = 0; i < n; i++) {
        fa [i] = i;
        r[i] = 0;
    }
}

int getf(int x) {
    if(x != fa[x]) {
        int rt = fa [x];
        fa [x] = getf (fa [x]);
        r[x] = (r[x] + r[rt]) % 3;
    }
    return fa[x];
}


intmain()
{
    int kcase = 0;
    int T;
    int a, b;
    init();
    int flag = 1;
    while(~scanf("%d %d", &n, &m)) {
        char c;
        for(int i = 0; i < m; i++) {
            scanf("%d", &q[i].a);
            c = getchar();
            while(c != '=' && c != '<' && c != '>') c = getchar();
            if(c == '=') q[i].c = 0;
            else if(c == '<') q[i].c = 1;
            else q[i].c = 2;
            scanf("%d", &q[i].b);
        }
        /*for(int i = 0; i < m; i++) {
            printf("%d %d %d\n", q[i].a, q[i].c, q[i].b);
        }*/
        memset(error, -1, sizeof(error));
        for(int i = 0; i < n; i++) {
            init();
            for(int j = 0; j < m; j++) {
                if(q[j].a == i || q[j].b == i) continue;
                int ra = getf (q [j] .a);
                int rb = getf(q[j].b);
                if(ra == rb) {
                    if(r[q[j].b] != (r[q[j].a] + q[j].c) % 3) {
                        error[i] = j + 1;
                        break;
                    }
                }
                else {
                    fa [rb] = ra;
                    r[rb] = ((r[q[j].a] + q[j].c - r[q[j].b]) % 3 + 3) % 3;
                }
            }
        }
        int cnt = 0;
        int a1, a2 = 0;
        for(int i = 0; i < n; i++) {
            if(error[i] == -1) {
                cnt++;
                a1 = i;
            }
            if(error[i] > a2) a2 = error[i];
        }
        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", a1, a2);
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324952564&siteId=291194637