Parity game POJ - 1733 带权并查集

Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the third to the fifth digit inclusively) and ask him, whether this subsequence contains even or odd number of ones. Your friend answers your question and you can ask him about another subsequence and so on. Your task is to guess the entire sequence of numbers.

You suspect some of your friend’s answers may not be correct and you want to convict him of falsehood. Thus you have decided to write a program to help you in this matter. The program will receive a series of your questions together with the answers you have received from your friend. The aim of this program is to find the first answer which is provably wrong, i.e. that there exists a sequence satisfying answers to all the previous questions, but no such sequence satisfies this answer.
Input
The first line of input contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 1000000000. In the second line, there is one positive integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5000. The remaining lines specify questions and answers. Each line contains one question and the answer to this question: two integers (the position of the first and last digit in the chosen subsequence) and one word which is either even' orodd’ (the answer, i.e. the parity of the number of ones in the chosen subsequence, where even' means an even number of ones andodd’ means an odd number).
Output
There is only one line in output containing one integer X. Number X says that there exists a sequence of zeroes and ones satisfying first X parity conditions, but there exists none satisfying X+1 conditions. If there exists a sequence of zeroes and ones satisfying all the given conditions, then number X should be the number of all the questions asked.
Sample Input
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
Sample Output
3

题意: A 写出一个01序列,长度为N;
B向A提出M个问题,每次给出 l,r ,询问区间 [ l, r ] 之间1 的个数为 odd or even ;
但是 A有可能会撒谎;
现在要确定这些答案,并确定至少在多少个回答后可以确定A撒谎,输出最小的数量 K;

思路:先离散化,然后将边赋上权值,边权 d[ x ]=0 ,表示 x 与 fa[ x ]奇偶性相同,那么我们在路径合并的时候,将所有路径上的权值做 xor 运算,即可得到 x 与根的关系;若在一个集合中,直接查询即可;不在一个集合,将这两个合并;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
using namespace std;
#define maxn 300005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
typedef __int64 ll;
typedef unsigned long long ull;
#define ms(x) memset(x,0,sizeof(x))
const long long int mod = 1e9 + 7;

inline int read()
{
    int x = 0, k = 1; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-')k = -1; c = getchar(); }
    while (c >= '0' && c <= '9')x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    return x * k;
}

struct  node { int l, r, ans; }query[maxn];
int a[maxn], fa[maxn], d[maxn], n, m;
int t;
void discrect() {
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        char ch[10];
        cin >> query[i].l >> query[i].r >> ch;
        if (ch[0] == 'o')query[i].ans = 1;
        else query[i].ans = 0;
        a[++t] = query[i].l - 1; a[++t] = query[i].r;

    }
    sort(a + 1, a + 1 + t);
    n = unique(a + 1, a + 1 + t) - a - 1;
}

int get(int x) {
    if (x == fa[x])return x;
    int root = get(fa[x]);
    d[x] ^= d[fa[x]];
    return fa[x] = root;
}


int main()
{
    ios::sync_with_stdio(false);
    discrect();
    for (int i = 0; i <= n; i++)fa[i] = i;
    for (int i = 1; i <= m; i++) {
        int x = lower_bound(a + 1, a + 1 + n, query[i].l - 1) - a - 1;
        int y = lower_bound(a + 1, a + 1 + n, query[i].r) - 1 - a;
        int p = get(x), q = get(y);
        if (q == p) {
            if ((d[x] ^ d[y]) != query[i].ans) {
                cout << i - 1 << endl; return 0;
            }
        }
        else {
            fa[p] = q;
            d[p] = d[x] ^ d[y] ^ query[i].ans;
        }
    }
    cout << m << endl;
}

猜你喜欢

转载自blog.csdn.net/qq_40273481/article/details/81777696
今日推荐