poj1733 Parity Game(扩展域并查集)

描述
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.
输入
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‘ orodd‘ (the answer, i.e. the parity of the number of ones in the chosen subsequence, where even‘ means an even number of ones andodd‘ means an odd number).
输出
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.
样例输入
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
样例输出
3
来源
CEOI 1999

题解:
使用类似于食物链的处理方法,设立两个n的点数,然后奇偶相互转化

#include <bits/stdc++.h>
#define int long long
using namespace std;
int fa[120000],n,m,a[120000],cnt;
char ch[120];
struct node {
    int l,r,which;
} query[120000];
int find(int x) {
    if(fa[x]==x) return x;
    return fa[x]=find(fa[x]);
}
signed main() {
    cin>>n>>m;
    for(int i=1;i<=m;i++) {
        scanf("%lld%lld%s",&query[i].l,&query[i].r,ch);
        query[i].which=(ch[0]=='o'?1:0);
        a[++cnt]=query[i].l-1;
        a[++cnt]=query[i].r;
    }
    sort(a+1,a+cnt+1);
    n=unique(a+1,a+cnt+1)-a-1;
    //a数组相当于一张字典,用来找自己的树离散化以后是谁
    for(int i=1;i<=n*2;i++) fa[i]=i;
    for(int i=1;i<=m;i++) {
        int x=lower_bound(a+1,a+n+1,query[i].l-1)-a,y=lower_bound(a+1,a+n+1,query[i].r)-a;
        int x_1=x,x_2=x+n,y_1=y,y_2=y+n;
        if(!query[i].which) {
            if(find(x_1)==find(y_2)) return cout<<i-1,0;
            fa[find(x_1)]=find(y_1);
            fa[find(x_2)]=find(y_2);
        } else {
            if(find(x_1)==find(y_1)) return cout<<i-1,0;
            fa[find(x_1)]=find(y_2);
            fa[find(x_2)]=find(y_1);
        }
    }
    cout<<m;
    return 0;
}

原文地址:https://www.cnblogs.com/wky32768/p/10746440.html

时间: 2024-10-09 21:21:28

poj1733 Parity Game(扩展域并查集)的相关文章

POJ2912 Rochambeau [扩展域并查集]

题目传送门 Rochambeau Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4463   Accepted: 1545 Description N children are playing Rochambeau (scissors-rock-cloth) game with you. One of them is the judge. The rest children are divided into three

AcWing:240. 食物链(扩展域并查集)

动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形. A吃B, B吃C,C吃A. 现有N个动物,以1-N编号. 每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是”1 X Y”,表示X和Y是同类. 第二种说法是”2 X Y”,表示X吃Y. 此人对N个动物,用上述两种说法,一句接一句地说出K句话,这K句话有的是真的,有的是假的. 当一句话满足下列三条之一时,这句话就是假话,否则就是真话. 1) 当前

【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) a

Noi2001 食物链(扩展域并查集)

4832: Noi2001 食物链 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 13  Solved: 12[Submit][Status][Web Board] Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是"1

并查集&amp;MST

[HDU] 1198 Farm Irrigation 基础最小生成树★ 1598 find the most comfortable road 枚举+最小生成树★★ 1811 Rank of Tetris 并查集+拓扑排序★★ 3926 Hand in Hand 同构图★ 3938 Portal 离线+并查集★★ 2489     Minimal Ratio Tree dfs枚举组合情况+最小生成树★ 4081     Qin Shi Huang's National Road System 最

【原创】并查集之扩展域与边带权

[前言] 并查集是一种可以动态维护若干个不重叠的集合,并支持合并于查询的数据结构. 并查集的基本概念很简单,但是这样一种思想的用途十分广泛. 个人理解:这是一种很巧妙的,可以很好的处理对象之间关系的数据结构. 那么先在这里提一下并查集的适用问题(划重点): 在一张无向图中维护节点之间的连通性或子图之间的连通性(图论优化) 动态维护许多具有传递性的关系(基本特性) 利用路径压缩来统计每个节点到树根之间路径上的一些信息(边带权) 维护具有多重关系的集合(扩展域) 以上基本上就是最高涉及到NOI级别难

「带权并查集」奇偶游戏

奇偶游戏 原题链接:奇偶游戏 题目大意 给你N个区间,每个区间给你它含1的奇偶性,问你哪些询问逻辑上冲突 题目题解 一道带权并查集的题,让我对带权并查集有了更深入的理解,带权并查集可以分为两种(在这道题中) "边带权"并查集 "扩展域"并查集 两种方法都是思维上的不同所造成的,其中第一种解法是最常见的,第二种解法在代码实现上是最简单的,我们先来对第一种进行探究 边带权,很明显,我们要在并查集的边上进行一个储存边权的操作,我们这里用d来表示当前节点到根节点的Xor值,

POJ1733 - Parity game - 并查集

这道题是带权并查集,只需要把权设成0或1就行,分别表示与根节点的关系.因为10亿个点,所以要用到离散化 #include<iostream> #include<stdio.h> #include<algorithm> using namespace std; int p[10005]; int dis[10005]; int ran[10005]; int tot; struct num { char oe[10]; int st,ed; int id; }po[500

51nod 1204 Parity(并查集应用)

1204 Parity 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串(例如其中的第3到第5个数字)问他,该子串中包含了奇数个还是偶数个1,他会回答你的问题,然后你可以继续提问......你怀疑朋友的答案可能有错,或说同他之前的答案相互矛盾,例如:1 - 2 奇数,3 - 4 奇数,那么可以确定1 - 4 一定是偶数,如果你的朋友回答是奇数,就产生了矛盾.给出所有你朋友的