Parity game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12121 | Accepted: 4688 |
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
解析:
如果用 数组表示序列
的前缀和,那么在每个回答中:
1. 有偶数个1,等价于
与
奇偶性不同。
2. 有奇数个1,等价于
与
奇偶性相同。
第一种方法是“边带权”并查集,边权为0,表示
与
奇偶性相同;为1表示就行不通。在路径压缩时,对
到树根路径上的边权做抑或运算,即可得到
与树根的奇偶性关系。
第二宗方法是“扩展域”并查集,把每个点分为两个节点分别表示:奇数域和偶数域。
代码(边带权):
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
using namespace std;
const int Max=10005;
int n,m,tot,ans,flag,fax,fay;
int father[Max<<1],d[Max],a[Max<<1];
struct shu{int l,r,tag;};
shu num[Max];
char ch[5];
inline int get_int()
{
int x=0,f=1;
char c;
for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
if(c=='-') f=-1,c=getchar();
for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
return x*f;
}
inline void pre()
{
tot=unique(a+1,a+tot+1)-a-1;
sort(a+1,a+tot+1);
for(int i=1;i<=n;i++) num[i].l=lower_bound(a+1,a+tot+1,num[i].l)-a,num[i].r=lower_bound(a+1,a+tot+1,num[i].r)-a;
}
inline int getfather(int v)
{
if(father[v]==v) return v;
int root=getfather(father[v]);
d[v]^=d[father[v]];
return father[v]=root;
}
int main()
{
scanf("%d%d",&m,&n);
for(int i=1;i<=n;i++)
{
a[++tot]=num[i].l=get_int(),a[++tot]=num[i].r=get_int()+1,scanf("%s\n",ch);
if(ch[0]=='e') num[i].tag=0;
else num[i].tag=1;
}
pre();
for(int i=1;i<=(n<<1);i++) father[i]=i;
for(int i=1;i<=n;i++)
{
fax=getfather(num[i].l),fay=getfather(num[i].r);
if(fax!=fay) father[fax]=fay,d[fax]=d[num[i].l]^d[num[i].r]^num[i].tag;
else if(d[num[i].l]^d[num[i].r] != num[i].tag) {ans=i-1;flag=1;break;}
}
if(flag) cout<<ans<<"\n";
else cout<<n<<"\n";
return 0;
}
代码(扩展域):
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
using namespace std;
const int Max=10005;
int n,m,tot,ans,flag,fax,fay;
int father[Max],d[Max],a[Max];
struct shu{int l,r,tag;};
shu num[Max];
char ch[5];
inline int get_int()
{
int x=0,f=1;
char c;
for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
if(c=='-') f=-1,c=getchar();
for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
return x*f;
}
inline void pre()
{
tot=unique(a+1,a+tot+1)-a-1;
sort(a+1,a+tot+1);
for(int i=1;i<=n;i++) num[i].l=lower_bound(a+1,a+tot+1,num[i].l)-a,num[i].r=lower_bound(a+1,a+tot+1,num[i].r)-a;
}
inline int get(int v){return father[v]==v ? father[v] : father[v]=get(father[v]);}
int main()
{
scanf("%d%d",&m,&n);
for(int i=1;i<=n;i++)
{
a[++tot]=num[i].l=get_int(),a[++tot]=num[i].r=get_int()+1,scanf("%s\n",ch);
if(ch[0]=='e') num[i].tag=0;
else num[i].tag=1;
}
pre();
for(int i=1;i<=(tot<<1);i++) father[i]=i;
for(int i=1;i<=n;i++)
{
int x1=num[i].l,y1=num[i].l+tot,x2=num[i].r,y2=num[i].r+tot;
if(num[i].tag)
{
if(get(x1) == get(x2) || get(y1) == get(y2)) {cout<<i-1;return 0;}
father[get(x1)]=get(y2),father[get(y1)]=get(x2);
}
else
{
if(get(x1) == get(y2) || get(y1) == get(x2)) {cout<<i-1;return 0;}
father[get(x1)]=get(x2),father[get(y1)]=get(y2);
}
}
cout<<n<<"\n";
return 0;
}