zoukankan      html  css  js  c++  java
  • POJ1733 Parity game —— 种类并查集

    题目链接:http://poj.org/problem?id=1733

    Parity game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 9806   Accepted: 3795

    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

     
     
     
    题解:
    1.由于数据的范围很大(1e9),但是个数最多只有1e4,所以可以用map对其进行离散化。
    2.此题相当于 POJ3038POJ2492 两题的综合。在此不作过多解释。
    3.注意:种类并查集的r[]必须全部初始化为0,而不能是其他数。换句话说,根节点的r[]必须保持恒为0
     
     
    代码如下:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 #define ms(a,b) memset((a),(b),sizeof((a)))
    13 using namespace std;
    14 typedef long long LL;
    15 const double EPS = 1e-8;
    16 const int INF = 2e9;
    17 const LL LNF = 2e18;
    18 const int MAXN = 1e5+10;
    19 
    20 int n, m;
    21 int fa[MAXN], r[MAXN];
    22 map<int, int> M;
    23 
    24 int find(int x)
    25 {
    26     if(fa[x]==-1) return x;
    27     int pre = find(fa[x]);
    28     r[x] = (r[x]+r[fa[x]])%2;
    29     return fa[x] = pre;
    30 }
    31 
    32 bool Union(int u, int v, int w)
    33 {
    34     int fu = find(u);
    35     int fv = find(v);
    36     if(fu==fv)
    37         return (r[v]-r[u]+2)%2 != w;
    38 
    39     fa[fv] = fu;
    40     r[fv] = (-r[v]+w+r[u]+2)%2;
    41     return false;
    42 }
    43 
    44 int main()
    45 {
    46     scanf("%d%d", &n, &m);
    47     M.clear();
    48     memset(fa, -1, sizeof(fa));
    49     memset(r, 0, sizeof(r));
    50 
    51     int ans = m, cnt = 0;
    52     for(int i = 1; i<=m; i++)
    53     {
    54         int u, v; char s[5];
    55         scanf("%d%d%s", &u, &v, s); u--;
    56         if(M.find(u)==M.end()) M[u] = ++cnt;
    57         if(M.find(v)==M.end()) M[v] = ++cnt;
    58         /**注意,0代表偶数, 1代表奇数。否则的话所有r[]都需初始化为1,
    59         这样也就导致了根节点的r[]也为1,而根节点的r[]必须为0,
    60         因为在路径压缩的时候会加上根节点的r[],如果根节点的
    61         r[]不为0, 那么路径上所有的点与根节点的关系都将发生了变化。
    62         **/
    63         if(ans==m && Union(M[u], M[v], (int)(s[0]=='o')))
    64             ans = i-1;
    65     }
    66     printf("%d
    ", ans);
    67 }
    View Code
  • 相关阅读:
    nRF5 SDK for Mesh(二) Getting started 快速开始
    QT 简单 TCP 通信,发送数据到服务器
    Bluetooth® Low Energy Beacons
    CC2540 低功耗串口, POWER_SAVING 模式 下 串口 0 的使用
    LWIP network interface 网卡 初始化 以 STM32 为例子 后面会有 用 2G 或者4G 模块 用 PPP拨号的 形式 虚拟出网卡 所以先以 这个为 前提
    R 语言入门
    Django 框架
    Windows 下 Django 安装
    windows 下搭建 git 服务器 copssh+git
    python Pystaller 将python文件打包成exe
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7661625.html
Copyright © 2011-2022 走看看