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-1000000000,而给出的描述最多不过5000,这样的话,就会产生一个问题,当处理完整个描述的时候,这是数组产生的空白会非常多。并且,你没法开这么大的数组。所以,说白了,你要压缩这些节点,恰恰这是并查集,就是给你一个区间吧[a,b]。这仅仅是说明了a与b有关系,这个关系是a,b。
接下来就是利用并查集的思想去优化了。
AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#include<map>
using namespace std;
typedef long long ll;
typedef double ld;
int pre[10010],sum[10010];
int N,M;
map<int,int>mp;
char s[10];
int Find(int x)
{
if(x==pre[x])
return x;
int t=Find(pre[x]);
sum[x]=(sum[x]+sum[pre[x]])%2;
return pre[x]=t;
}
int judge(int x,int y,int z)
{
int fx=Find(x),fy=Find(y);
if(fx==fy)
{
if((sum[x]+sum[y]+z)&1)
return 0;
return 1;
}
pre[fy]=fx;
sum[fy]=((sum[x]+sum[y]+z)&1)? 1:0;
return 1;
}
int main()
{
cin>>N>>M;
for(int i=0; i<10010; i++)
pre[i]=i,sum[i]=0;
int a,b,cnt,tot=0;
for(cnt=0; cnt<M; cnt++)
{
scanf("%d %d %s",&a,&b,s);
a--;
if(mp.count(a)==0)
mp[a]=tot++;
if(mp.count(b)==0)
mp[b]=tot++;
int d=(s[0]=='e')? 0:1;
if(judge(mp[a],mp[b],d))
continue;
break;
}
cout<<cnt<<endl;
return 0;
}