【POJ1733】【带标记并查集】Parity game

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

Source

【分析】
很基本的东西,不会的话先去做食物链把,注意因为是sum[b] - sum[a - 1], a - 1会到0,所以反过来b要+1
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <map> const int MAXN = + ;
const int MAX = + ;
using namespace std;
struct DATA{
int num, order;
bool operator < (DATA b)const{
return num < b.num;
}
}data[MAXN * ];//data记录的是出现过的数字
struct DATA2{
int a, b, type;//表示sum[b] - sum[a - 1]的是否是同一类型
}data2[MAXN];//直接的输入数据
int parent[MAXN * ];
int val[MAXN * ]; int n, q, cnt; void init(){
scanf("%d%d", &n, &q);
for (int i = ; i <= q; i++){
char str[];
scanf("%d%d", &data2[i].a, &data2[i].b);
data[i * - ].num = data2[i].a; data[i * - ].order = i * - ;
data[i * ].num = data2[i].b; data[i * ].order = i * ;
scanf("%s", str);
//odd是奇数,even是偶数
if (str[] == 'o') data2[i].type = ;
else if (str[] == 'e') data2[i].type = ;
}
//准备离散化
sort(data + , data + + * q);
cnt = ;//记录数字的
data[].num = -;
for (int i = ; i <= * q; i++){
if (data[i].num != data[i - ].num) ++cnt;
if (data[i].order % == ) data2[((data[i].order - ) / ) + ].a = cnt;
else data2[((data[i].order - ) / ) + ].b = cnt;
}
/*for (int i = 1;i <= q; i++){
printf("%d %d %d\n", data2[i].a, data2[i].b, data2[i].type);
}*/
}
int find(int x){
int tmp = , f = x;
//注意要加上路径压缩
while (x != parent[x]){
tmp += val[x];
x = parent[x];
}
parent[f] = x;
val[f] = tmp % ;
return x;
} void work(){
//val[i] = 1则与父亲的奇偶性不同,0则相同
for (int i = ; i <= cnt; i++){
parent[i] = i;
val[i] = ;
}
for (int i = ; i <= q; i++){
int x = data2[i].a, y = data2[i].b ,t = data2[i].type;
y++;
if (i == )
printf("");
int xx = find(x), yy = find(y), flag = ;
if (xx == yy){//同根
if (t == ){//需要相同
if (val[x] != val[y]) flag = ;
//相同则躲过一劫
}else{//需要不同
if (val[x] == val[y]) flag = ;
}
}else{//不管怎么样都要合并
if (t == ){
parent[xx] = y;
val[xx] = (-val[x] + ) % ;
}else{
parent[xx] = y;
val[xx] = (-val[x] + ) % ;
}
}
if (flag == ) {printf("%d\n", i - );return;}
}
printf("%d\n", q);
} int main(){
int T;
#ifdef LOCAL
freopen("data.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
init();
work();
return ;
}

 
上一篇:公共资源情报(OSINT)工具Automater


下一篇:eNSP仿真学习和VLAN配置