• 【POJ1733】【带标记并查集】Parity game


    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

    【分析】
    很基本的东西,不会的话先去做食物链把,注意因为是sum[b] - sum[a - 1], a - 1会到0,所以反过来b要+1
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <algorithm>
      4 #include <cstring>
      5 #include <vector>
      6 #include <utility>
      7 #include <iomanip>
      8 #include <string>
      9 #include <cmath>
     10 #include <queue>
     11 #include <map>
     12 
     13 const int MAXN = 5000 + 10; 
     14 const int MAX = 32000 + 10; 
     15 using namespace std;
     16 struct DATA{
     17        int num, order;
     18        bool operator < (DATA b)const{
     19             return num < b.num;
     20        }
     21 }data[MAXN * 2];//data记录的是出现过的数字
     22 struct DATA2{
     23        int a, b, type;//表示sum[b] - sum[a - 1]的是否是同一类型 
     24 }data2[MAXN];//直接的输入数据 
     25 int parent[MAXN * 2];
     26 int val[MAXN * 2];
     27 
     28 int n, q, cnt;
     29 
     30 void init(){
     31      scanf("%d%d", &n, &q);
     32      for (int i = 1; i <= q; i++){
     33           char str[10];
     34           scanf("%d%d", &data2[i].a, &data2[i].b);
     35           data[i * 2 - 1].num = data2[i].a; data[i * 2 - 1].order = i * 2 - 1;
     36           data[i * 2].num = data2[i].b; data[i * 2].order = i * 2;
     37           scanf("%s", str);
     38           //odd是奇数,even是偶数 
     39           if (str[0] == 'o') data2[i].type = 0;
     40           else if (str[0] == 'e') data2[i].type = 1; 
     41      }
     42      //准备离散化
     43      sort(data + 1, data + 1 + 2 * q);
     44      cnt = 0;//记录数字的 
     45      data[0].num = -100;
     46      for (int i = 1; i <= 2 * q; i++){
     47          if (data[i].num != data[i - 1].num) ++cnt;
     48          if (data[i].order % 2 == 1) data2[((data[i].order - 1) / 2) + 1].a = cnt;
     49          else data2[((data[i].order - 1) / 2) + 1].b = cnt;
     50      }  
     51      /*for (int i = 1;i <= q; i++){
     52          printf("%d %d %d
    ", data2[i].a, data2[i].b, data2[i].type);
     53      }*/
     54 }
     55 int find(int x){
     56     int tmp = 0, f = x;
     57     //注意要加上路径压缩 
     58     while (x != parent[x]){
     59           tmp += val[x];
     60           x = parent[x];
     61     }
     62     parent[f] = x;
     63     val[f] = tmp % 2;
     64     return x;  
     65 }
     66 
     67 void work(){
     68      //val[i] = 1则与父亲的奇偶性不同,0则相同 
     69      for (int i = 1; i <= cnt; i++){
     70          parent[i] = i;
     71          val[i] = 0;
     72      }
     73      for (int i = 1; i <= q; i++){
     74          int x = data2[i].a, y = data2[i].b ,t = data2[i].type;
     75          y++;
     76          if (i == 2)
     77          printf("");
     78          int xx = find(x), yy = find(y), flag = 0;
     79          if (xx == yy){//同根 
     80             if (t == 1){//需要相同 
     81                if (val[x] != val[y]) flag = 1;
     82                //相同则躲过一劫 
     83             }else{//需要不同 
     84                if (val[x] == val[y]) flag = 1;
     85             }
     86          }else{//不管怎么样都要合并 
     87             if (t == 1){
     88                parent[xx] = y;
     89                val[xx] = (-val[x] + 2) % 2;
     90             }else{
     91                parent[xx] = y;
     92                val[xx] = (-val[x] + 3) % 2;
     93             }
     94          }
     95          if (flag == 1) {printf("%d
    ", i - 1);return;}
     96      }
     97      printf("%d
    ", q);
     98 }
     99 
    100 int main(){
    101     int T;
    102     #ifdef LOCAL
    103     freopen("data.txt",  "r",  stdin);
    104     freopen("out.txt",  "w",  stdout); 
    105     #endif
    106     init();
    107     work();
    108     return 0;
    109 }
    View Code
     
  • 相关阅读:
    自我介绍
    oracle 几种分页
    for update nowait
    彻底卸载SQL 2005
    如何将 SQL SERVER 彻底卸载干净
    iBatis 到 MyBatis区别
    SqlMapConfig.xml配置文件详解
    mybatis简单应用(基于配置文件)
    ibatis学习笔记一:sqlMapConfig.xml文件配置详解
    Extjs 中的添加事件总结
  • 原文地址:https://www.cnblogs.com/hoskey/p/4321802.html
Copyright © 2020-2023  润新知