Parity game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9400 | Accepted: 3645 |
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.
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
题目分析:首先因为数据量很大的关系,肯定要离散化一下数据。在这里可以用一个map<int,int>来存储出现的数,并且离散。然后呢,因为是判断区间(a,b)奇偶性,然后区间具有包容性和合并性,由此可以想到带权并查集。如果一个区间为偶,则赋值为1,即a-->b路径上的权值为val[a]=1。然后考虑合并性。如有(a,b),(b,d)两个区间。那么就可以a-->b,b-->c。然后合并得a-->c且val[a] = (val[a]+val[b])%2.根据这两条性质便可以得到答案。
代码:
/* Author:kzl */
#include<iostream>
#include <cstdio>
#include <map>
using namespace std;
const int maxn = 10010 ;
int fa[maxn], val[maxn] ;
void init() {
for (int i = 0; i < maxn; i++) {
fa[i] = i;
val[i] = 0;
}
}
int find (int x) {
if (fa[x] == x) {
return x;
}
int root = find(fa[x]);
val[x] = (val[x] + val[fa[x]]) % 2;
fa[x] = root;
return fa[x];
}
int Uni(int x, int y, int d){
int a = find(x) ;
int b = find(y) ;
if (a == b) {
if ((val[x] - val[y] + 4) % 2 == d) return 1;
else return 0 ;
} else {
fa[a] = b ;
val[a] = (val[y] - val[x] + d + 4) % 2;
return 1 ;
}
}
int main() {
char s[5] ;
int n, m ;
scanf("%d%d", &n, &m) ;
init() ;
map <int, int> ma ;
int i, a, b, add = 0, d ;
for (i = 1 ; i <= m ; i++) {
scanf("%d %d %s", &a, &b,s);
a--;
if (ma.find(a) == ma.end()) ma[a] = add++;
int x = ma[a] ;
if (ma.find(b) == ma.end()) ma[b] = add++;
int y = ma[b] ;
if (s[0] == 'o') d = 1 ;
else d = 0 ;
if (Uni(x, y, d) == 0) break ;
}
printf("%d\n", i - 1) ;
return 0;
}