[POJ1733]Parity game

Parity game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9452   Accepted: 3656

Description

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' 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).

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

大致题意:

一个由0,1组成的数字串,现在你问一个人,第i位到第j位的1的个数为奇数还是偶数。一共会告诉你几组这样的数

要你判断前k组这个人回答的都是正确的,到第k+1组,这个人说的是错的,要你输出这个k,要是这个人回答的都是正确的,则输出组数。odd为奇数,even为偶数。

题解:

询问01区间中1的个数的奇偶性等价于询问区间和的奇偶性。

容易想到用前缀和求区间和sum[l,r]=sum[r]-sum[l-1]。此时,若sum[l,r]为奇,说明sum[r]与sum[l-1]奇偶性相反,否则sum[r]与sum[l-1]奇偶性相同。

此时问题转换为询问sum[r]与sum[l-1]的奇偶性是否冲突。

考虑用权值并查集维护。对于每一个点,除了并查集的fa之外,额外维护一个v。若v=0则说明该店与其父节点奇偶性相同,v=1表示不同。

维护此并查集即可得出答案。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 int s[5005],t[5005];;
 8 char str[5005][10];
 9 int a[10005];
10 int fa[10005],v[10005];
11 int n,q,cnt;
12 int find(int x)
13 {
14     if(x!=fa[x])
15     {
16         int f=fa[x];
17         fa[x]=find(fa[x]);
18         v[x]=v[x]^v[f];
19     }
20     return fa[x];
21 }
22 int main()
23 {
24     scanf("%d%d",&n,&q);
25     cnt=0;
26     for(int i=1;i<=q;i++)
27     {
28         scanf("%d%d%s",&s[i],&t[i],str[i]);
29         a[++cnt]=s[i]-1;a[++cnt]=t[i];
30     }
31     sort(a+1,a+cnt+1);
32     cnt=unique(a+1,a+cnt+1)-a-1;
33     for(int i=1;i<=cnt;i++ )fa[i]=i;
34     int ans=0;
35     for(int i=1;i<=q;i++)
36     {
37         int a1=lower_bound(a+1,a+cnt+1,s[i]-1)-a,b1=lower_bound(a+1,a+cnt+1,t[i])-a;
38         int fa1=find(a1),fb1=find(b1);
39         if(fa1==fb1)
40         {
41             if(v[a1]!=v[b1]&&str[i][0]=='e'){printf("%d",ans);return 0;}
42             if(v[a1]==v[b1]&&str[i][0]=='o'){printf("%d",ans);return 0;}
43         }
44         else
45         {
46             if(str[i][0]=='e')
47             {
48                 fa[fa1]=fb1;
49                 if(v[a1]==v[b1]) v[fa1]=0;
50                 else v[fa1]=1;
51             }
52             else
53             {
54                 fa[fa1]=fb1;
55                 if(v[a1]==v[b1]) v[fa1]=1;
56                 else v[fa1]=0;
57             }
58         }
59         ans++;
60     }
61     printf("%d",ans);
62 }
View Code
O(∩_∩)O~ (*^__^*) 嘻嘻…… O(∩_∩)O哈哈~
原文地址:https://www.cnblogs.com/wls001/p/7327730.html