URAL

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
Input contains a series of tests. The first line of each test contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 10 9. In the second line, there is one non-negative integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5 000. 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” or “ odd” (the answer, i.e. the parity of the number of ones in the chosen subsequence, where “ even” means an even number of ones and “ odd” means an odd number). The input is ended with a line containing −1 .
Output
Each line of 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.
Example
inputoutput
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
-1

题意:N个点,给出M组关系,每组给出[L,R]的奇偶,问前几个是没有矛盾的。

思路:我们用[L-1,R]连边边权为其奇偶性,如果出现了全为1的奇环,那么就出现矛盾了。所以可以用带权并查集来做。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=10010;
int L[maxn],R[maxn],val[maxn],fa[maxn],sum[maxn];
int b[maxn],tot,ans; char s[10];
int find(int x){
    if(x==fa[x]) return x;
    int tf=fa[x]; fa[x]=find(fa[x]);
    sum[x]^=sum[tf]; return fa[x];
}
int main()
{
    int N,M;
    while(~scanf("%d",&N)){
        if(N==-1) break;
        scanf("%d",&M); tot=ans=0;
        rep(i,1,M) {
            scanf("%d%d%s",&L[i],&R[i],s); L[i]--;
            b[++tot]=L[i]; b[++tot]=R[i];
            if(s[0]=='e') val[i]=0;
            else val[i]=1;
        }
        sort(b+1,b+tot+1); tot=unique(b+1,b+tot+1)-(b+1);
        rep(i,1,M) {
            L[i]=lower_bound(b+1,b+tot+1,L[i])-b;
            R[i]=lower_bound(b+1,b+tot+1,R[i])-b;
        }
        rep(i,1,tot) fa[i]=i,sum[i]=0;
        rep(i,1,M){
            int tu=find(L[i]),tv=find(R[i]);
            if(tu==tv){
                if((sum[L[i]]^sum[R[i]])!=val[i]) break;
            }
            else {
                fa[tu]=tv,sum[tu]=sum[R[i]]^sum[L[i]]^val[i];
            }
            ans=i;
        }
        printf("%d
",ans);
    }
    return 0;
}
//不能有奇环!用带权并查集来优化2-sat就是这么来的。
原文地址:https://www.cnblogs.com/hua-dong/p/10067559.html