ural 1003 Parity 并查集

来源:互联网 发布:淘宝哪个店能寄到韩国 编辑:程序博客网 时间:2024/06/08 14:54

http://acm.timus.ru/problem.aspx?space=1&num=1003
1003. Parity
Time limit: 2.0 second
Memory limit: 64 MB
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
Input contains a series of tests. The first line of each test contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 109. In the second line, there is one non-negative integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5 000. 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). The input is ended with a line containing −1.
Output
Each line of 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
-1
output
3

题意:
一个由0和1组成的序列,告诉你n条信息,表示区间内1个数的奇偶性,问你在最多第几句话前没出现矛盾。

题解:
将问题的区间和转化为两个前缀和之差(这类思想经常用到),如果是偶那么两个前缀和奇偶相同,应放在一个集合,如果为奇应该放在不同集合。
果a,b在一个集合里 那么a和b+block,b和a+block肯定不在一个集合里,a+block和b+block在一个集合里。
如果a,b不在一个集合里 那么a和b+block,b和a+block肯定在一个集合里。
block是一个很大的数 起码要比单词的长度长,它的作用就是证明a,b在或者不在一个集合里,但是由于题目中序列的最大长度很大,但由于操作很少,所以可以用hash或者map离散化。
对于a,b如果给的是even那么先判a,b是不是在一个集合里,不是不在的话(可能未知),merge(a,b),merge(a+block,b+block);
如果给的是odd那么先判a,b是不是不在一个集合里,不是在的话,merge(a,b+block),merge(a+block,b);

其实我一直很自恋地认为自己的程序一直写地很简短
程序

#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cstring>#include<map>using namespace std;const int block=50003;int fa[100100];int find(int x){    return fa[x]= (fa[x]==x) ? x : find(fa[x]);}int merge(int x, int y){    int fx=find(x), fy=find(y);    if (fx!=fy) fa[fx]=fy;}char c[10];int x, y;int main(){    int len, n;    while (scanf("%d", &len)!=EOF && len!=-1)    {        scanf("%d", &n);        for (int i=1; i<=block*2; i++) fa[i]=i;        fa[0]=1;        int ans=-1;        for (int i=1; i<=n; i++){            scanf("%d%d", &x, &y);            scanf("%s", c);            if (ans!=-1) continue;            x=(x-1)%block; y=y%block;            if (c[0]=='e'){                if (find(x)==find(y+block)) ans=i-1; else{                merge(x, y);                merge(x+block, y+block);}            }else{                if (find(x)==find(y+block)) ans=i-1;else{                merge(x, y+block);                merge(x+block, y);}            }        }        if (ans==-1) ans=n;        printf("%d\n", ans);    }    return 0;}
0 1
原创粉丝点击