zoukankan      html  css  js  c++  java
  • POJ 1733 Parity game (离散化+带权并查集)

    Parity game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7016   Accepted: 2728

    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

     
    题意:由N个点组成的区间内有M个描述,求第一次与前面描述不符合是第几组描述,如果全部描述正确,输出组数。
    分析:和hdu 3047差不多的题目,这题点范围很大,但是只有5000次查询,最多也就5000*2个点,所以要离散化缩小点的范围
    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<string>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<vector>
    #include<map>
    #include<stdlib.h>
    #include<algorithm>
    #define LL __int64
    using namespace std;
    
    const int MAXN=10000+10;
    int n,m,cnt,ans;
    int w[MAXN],p[MAXN];
    map<int,int> mat;
    
    void init()
    {
        memset(w,0,sizeof(w));
        memset(p,-1,sizeof(p));
        mat.clear();
        cnt=0;
        ans=m;
    }
    
    int findfa(int x)
    {
        if(p[x]==-1) return x;
        int tmp=p[x];
        p[x]=findfa(p[x]);
        w[x]^=w[tmp];
        return p[x];
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(scanf("%d %d",&n,&m)!=EOF)
        {
            init();
            for(int i=0;i<m;i++)
            {
                int u,v,num;
                char str[10];
                scanf("%d %d %s",&u,&v,str);
                u--;
                if(ans<m) continue;  
    
                if(mat.find(u)==mat.end()) mat[u]=cnt++;
                if(mat.find(v)==mat.end()) mat[v]=cnt++;
    
                if(str[0]=='o') num=1;
                else num=0;
    
                u=mat[u];
                v=mat[v];
                int x=findfa(u);
                int y=findfa(v);
                if(x==y)
                {
                    if(w[u]^w[v]!=num)
                        ans=i;
                }
                else
                {
                    p[y]=x;
                    w[y]=w[u]^w[v]^num;
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    python3编译安装no module named _ssl
    java中的、标识符、运算符以及数据类型之间的转换。
    Java中的基本数据类型
    java中的类和对象
    什么是JDK,JRE
    HTML是什么
    test
    Obtain older GMT versions
    How to understand three foundanmental faults?
    wilber3申请数据的直接目录寻找
  • 原文地址:https://www.cnblogs.com/clliff/p/4698687.html
Copyright © 2011-2022 走看看