poj 1733Parity game(map离散+带权并查集)

来源:互联网 发布:淘宝卖家数据分析 编辑:程序博客网 时间:2024/05/17 03:36
Parity game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7567 Accepted: 2951

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

1051 2 even3 4 odd5 6 even1 6 even7 10 odd

Sample Output

3


题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾

[l,r]中1个个数可以表示为sum[r]-sum[l-1],而题目只要求是奇偶,也就确定了sum[r]与sum[l-1]奇偶是否相同,到了这步就简单了,转变为经典模型

范围有点大,按l-1,r离散化一下



#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <string>#include <cmath>#include <set>#include <queue>#include <algorithm>#include <vector>#include <stack>#include <map>using namespace std;#define esp  1e-8const double PI = acos(-1.0);const long long inf = 1000000000;const long long mod = 10000007;typedef long long LL;#pragma comment(linker, "/STACK:1024000000,1024000000") //freopen("in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取//freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中int f[30005];int v[30005];int a[30005], b[30005];char s[30005][10];set<int>st;set<int>::iterator it;map<int, int>mp;int find(int x){if (x != f[x]){int u = f[x];f[x] = find(f[x]);v[x] = (v[u] + v[x]) % 2;}return f[x];}int main(){int n, m, i, j;while (~scanf("%d%d", &n, &m)){int ans = 0;int res = 0;st.clear();mp.clear();for (i = 1; i <= m; ++i){scanf("%d%d%s", &a[i], &b[i], s[i]);st.insert(a[i]);st.insert(b[i]);}int nn = 0;for (it = st.begin(); it != st.end(); ++it){mp[*it] = ++nn;}for (i = 0; i <= nn; ++i)f[i] = i;memset(v, 0, sizeof(v));v[0] = 0;for (i = 1; i <= m; ++i){int fa = find(mp[a[i]] - 1);int fb = find(mp[b[i]]);if (fa == fb)//如果相同,则已经知道了mp[a[i]] - 1和mp[b[i]]的奇偶,所以直接判断即可{if (v[mp[a[i]] - 1] == v[mp[b[i]]] && s[i][0] == 'o')break;if (v[mp[a[i]] - 1] != v[mp[b[i]]] && s[i][0] == 'e')break;ans++;}else{f[fa] = fb;if (s[i][0] == 'o') //如果是奇数,需要加上1{v[fa] = (v[mp[a[i]] - 1] + v[mp[b[i]]] + 1) % 2;}else{v[fa] = (v[mp[a[i]] - 1] + v[mp[b[i]]]) % 2;}ans++;}}printf("%d\n", ans);}}



0 0