码迷,mamicode.com
首页 > 其他好文 > 详细

Parity

时间:2016-08-16 23:39:19      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

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

inputoutput
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
-1
3

 

分析:带权并查集,注意离散化;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e5+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,k,t,id,p[maxn],a[maxn],s[maxn],d[maxn],q[maxn],v[maxn];
char b[10];
int find(int x)
{
    if(x!=p[x])
    {
        int fa=find(p[x]);
        a[x]^=a[p[x]];
        p[x]=fa;
    }
    return p[x];
}
bool join(int x,int y,int z)
{
    int px=find(x),py=find(y);
    if(px!=py)
    {
        p[py]=px;
        a[py]=a[y]^a[x]^z;
        return false;
    }
    return true;
}
int main()
{
    int i,j;
    while(~scanf("%d",&t)&&t!=-1)
    {
        scanf("%d",&n);
        memset(a,0,sizeof(a));
        j=1;
        rep(i,1,n)scanf("%d%d%s",&s[i],&d[i],b),q[j++]=s[i],q[j++]=d[i],v[i]=(b[0]==o);
        sort(q+1,q+j+1);
        int num=unique(q+1,q+j+1)-q-1;
        rep(i,1,n)
        {
            s[i]=lower_bound(q+1,q+num+1,s[i])-q-2;
            d[i]=lower_bound(q+1,q+num+1,d[i])-q-1;
        }
        rep(i,0,num)p[i]=i;
        rep(i,1,n)
        {
            if(join(s[i],d[i],v[i]))
            {
                if(a[s[i]]^a[d[i]]!=v[i])break;
            }
        }
        printf("%d\n",i-1);
    }
    //system("pause");
    return 0;
}

Parity

标签:

原文地址:http://www.cnblogs.com/dyzll/p/5778129.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!