并查集
poj 1733
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.
10 5 1 2 even 3 4 odd 5 6 even 1 6 even 7 10 oddSample Output
3
题目大意:找到至少多少次回答后A一定撒谎
题解:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 20010;
int a[maxn],fa[maxn],d[maxn],n,m,t;
struct
{
int l,r,ans;
}query[maxn];
void read_dis()
{
cin>>n>>m;
for(int i =1 ;i<=m;i++)
{
char str[5];
scanf("%d%d%s",&query[i].l,&query[i].r,str);
query[i].ans = (str[0] == 'o'?1:0);
a[++t] = query[i].l-1;
a[++t] = query[i].r;
}
sort(a+1,a+t+1);
n = unique(a+1,a+t+1)-a-1;
}
int find(int x)
{
if(x == fa[x]) return x;
return fa[x] = find(fa[x]);
}
int main ()
{
read_dis();
for(int i= 1;i<=n*2;i++)
fa[i] = i;
for(int i =1;i<=m;i++)
{
int x= lower_bound(a+1,a+n+1,query[i].l-1)-a;
int y = lower_bound(a+1,a+n+1,query[i].r) - a;
int x_odd = x;
int x_even = x+n;
int y_odd = y;
int y_even = y+n;
if(query[i].ans == 0)
{
if(find(x_odd) == find(y_even))
{
cout<<i-1<<endl;
return 0;
}
fa[find(x_odd)] = find(y_odd);
fa[find(x_even)] = find(y_even);
}
else
{
if(find(x_odd) == find(y_odd))
{
cout<<i-1<<endl;
return 0;
}
fa[find(x_odd)] = find(y_even);
fa[find(x_even)] = find(y_odd);
}
}
cout<<m<<endl;
return 0 ;
}