並查集------H - Parity game
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
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
和帶權並查集專題前面的D題類似,都是線段區間,但是這個長度10^11,空間太大。。。。
這裡有一個離散化壓縮空間的方法,其實做線段樹題的時候經常碰到這樣的做法,不會靈活運用,太菜了。。。
注意:unique函式只在有序的陣列中起作用
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1);
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int N = 5005;
int b[N];
int r[N];
vector<int>ve;
typedef struct Node{
int u,v,w;
}Node;
Node node[N];
int join(int x)
{
int k = b[x];
if(b[x] != x){
b[x] = join(b[x]);
r[x] = (r[x] + r[k]) % 2;
}
return b[x];
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
ve.clear();
for(int i = 0;i < m;++i){
char s[15];
scanf("%d %d %s",&node[i].u,&node[i].v,s);
node[i].w = s[0] == 'e' ? 0 : 1;
ve.pb(node[i].u - 1);ve.pb(node[i].v);
}
sort(ve.begin(),ve.end());
ve.erase(unique(ve.begin(),ve.end()),ve.end());
int len = ve.size();
for(int i = 0;i < len;++i){
b[i] = i;
r[i] = 0;
}
bool flag = false;
for(int i = 0;i < m;++i){
int u = lower_bound(ve.begin(),ve.end(),node[i].u - 1) - ve.begin();
int v = lower_bound(ve.begin(),ve.end(),node[i].v) - ve.begin();
int p = join(u),q = join(v);
if(p != q){
b[p] = q;
r[p] = (node[i].w + r[u] + r[v]) % 2;
}else if(p == q && (r[u] + node[i].w) % 2 != r[v]){
flag = true;
printf("%d\n",i);
break;
}
}
if(!flag){
printf("%d\n",m);
}
}
return 0;
}