并查集------H - Parity game

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

和带权并查集专题前面的D题类似,都是线段区间,但是这个长度10^11,空间太大。。。。
这里有一个离散化压缩空间的方法,其实做线段树题的时候经常碰到这样的做法,不会灵活运用,太菜了。。。
注意:unique函数只在有序的数组中起作用

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1);
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int N = 5005;

int b[N];
int r[N];
vector<int>ve;
typedef struct Node{
    int u,v,w;
}Node;
Node node[N];

int join(int x)
{
    int k = b[x];
    if(b[x] != x){
        b[x] = join(b[x]);
        r[x] = (r[x] + r[k]) % 2;
    }
    return b[x];
}

int main()
{
    int n,m;
    while(~scanf("%d %d",&n,&m))
    {
        ve.clear();
        for(int i = 0;i < m;++i){
            char s[15];
            scanf("%d %d %s",&node[i].u,&node[i].v,s);
            node[i].w = s[0] == 'e' ? 0 : 1;
            ve.pb(node[i].u - 1);ve.pb(node[i].v);
        }
        sort(ve.begin(),ve.end());
        ve.erase(unique(ve.begin(),ve.end()),ve.end());
        int len = ve.size();
        for(int i = 0;i < len;++i){
            b[i] = i;
            r[i] = 0;
        }
        bool flag = false;
        for(int i = 0;i < m;++i){
            int u = lower_bound(ve.begin(),ve.end(),node[i].u - 1) - ve.begin();
            int v = lower_bound(ve.begin(),ve.end(),node[i].v) - ve.begin();
            int p = join(u),q = join(v);
            if(p != q){
                b[p] = q;
                r[p] = (node[i].w + r[u] + r[v]) % 2;
            }else if(p == q && (r[u] + node[i].w) % 2 != r[v]){
                flag = true;
                printf("%d\n",i);
                break;
            }
        }
        if(!flag){
            printf("%d\n",m);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36386435/article/details/83016574