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

题意

  一个由0,1组成的序列,每次给出一段区间的奇偶,问哪一条信息不合法。

Solution

  并查集。

  题目中序列的长度有很大,单纯搜索一定会TLE。

  我们用s[i]表示前i个数的前缀和,那么a b even意味着s[b]和s[a-1]的奇偶性相同。a b odd意味着s[b]与s[a-1]的奇偶性不同。于是我们根据奇偶性的不同,用并查集依次处理他们之间的关系。当某条信息出现与并查集中记录的信息不符合时,则此信息不合法。

  由于a和b的值可能会非常大,所以我们还需要将它离散化。

代码

// poj1733
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define Pi 3.1415926535898
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; int n,m,p[1000010],fa[1000010],s[1000010],x[1000010],y[1000010],pos[1000010],cnt;
char ch[10]; int binsearch(int x) {
int tl=1,tr=cnt;
while (tl<=tr) {
int m=(tl+tr)>>1;
if (x==pos[m]) return m;
if (x>pos[m]) tl=m+1;
else tr=m-1;
}
}
int find(int x) {
if (fa[x]==x) return x;
int f=find(fa[x]);
s[x]=(s[fa[x]]+s[x])&1;
fa[x]=f;
return fa[x];
}
void unnion(int i) {
s[fa[x[i]]]=(s[x[i]]+p[i])&1;
fa[fa[x[i]]]=y[i];
}
int main() {
scanf("%d%d",&m,&n);
for (int i=1;i<=n;i++) {
scanf("%d%d",&x[i],&y[i]);
scanf("%s",ch);
if (ch[0]=='o') p[i]=1;
x[i]--;
pos[++cnt]=x[i];
pos[++cnt]=y[i];
}
sort(pos+1,pos+1+cnt);
pos[0]=1;
for (int i=2;i<=cnt;i++) if (pos[i]!=pos[pos[0]]) {pos[0]++;pos[pos[0]]=pos[i];}
cnt=pos[0];
for (int i=1;i<=cnt;i++) fa[i]=i;
for (int i=1;i<=n;i++) {
x[i]=binsearch(x[i]);
y[i]=binsearch(y[i]);
if (find(x[i])==find(y[i])) {if (((s[x[i]]+s[y[i]])&1)!=p[i]) {printf("%d",i-1);return 0;}}
else unnion(i);
}
printf("%d",n);
return 0;
}

  

【poj1733】 Parity game的更多相关文章

  1. 【POJ1733】Parity game

    [POJ1733]Parity game 题面 vjudge 题解 比较简单的分类并查集 将一个查询操作看作前缀和\(s_r-s_{l-1}\)的奇偶性 将每个点拆成一奇一偶然后分别连边即可 如果一个 ...

  2. 【poj1733】Parity game--边带权并查集

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15776   Accepted: 5964 Description Now ...

  3. 【POJ1733】【带标记并查集】Parity game

    Description Now and then you play the following game with your friend. Your friend writes down a seq ...

  4. 【Codeforces Global Round 1 A】Parity

    [链接] 我是链接,点我呀:) [题意] 给你一个k位数b进制的进制转换. 让你求出来转成10进制之后这个数字是奇数还是偶数 [题解] 模拟一下转换的过程,加乘的时候都记得对2取余就好 [代码] im ...

  5. 【POJ 1733】 Parity Game

    [题目链接] http://poj.org/problem?id=1 [算法] 并查集 [代码] #include <algorithm> #include <bitset> ...

  6. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  7. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  8. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  9. 【C#】串口操作实用类

    做工业通 信有很长时间了,特别是串口(232/485),有VB/VC/C各种版本的串口操作代码,这些代码也经过了多年的现场考验,应该说是比较健壮的代码,但 是目前却没有C#相对成熟的串口操作代码,最近 ...

随机推荐

  1. oracle:如何用sql生成日历

    BI分析中,经常需要将事实表与时间维度表关联起来,按年/月/日来逐层展示,常用的做法是创建一张日历表,结构类似如下: create table T_BAS_CALENDAR ( d_year ) no ...

  2. Putty颜色设置

    默认的Putty颜色和字体太不好看了,得自己设置: 字体:毫无疑问Consolas, 10-point:看起来非常清新自然 颜色: * Default Foreground: 255/255/255  ...

  3. [转]Android Studio创建Xposed模块项目时BridgeApi的正确添加方式

    使用Android Studio创建的空项目作为Xposed Module App,对于Api Jar包的引用方式,一开始是按照傻瓜式Jar Lib的处理方式,复制XposedBridgeApi-54 ...

  4. osx开发,skport项目记录

    最近在研究前端框架,学习了一下vue.js,而后找到了element.js,后来又了解到了cooking···前端开发真是三天小更新,一周大变样,一个月天翻地覆啊··· 期间在使用cooking时遇到 ...

  5. [BZOJ1232][[Usaco2008Nov]安慰奶牛cheer(MST)

    题目:http://hzwer.com/2493.html 分析:对于每条边,贡献的价值是这条边的边权加上这条边连接的两点的权值,所以可以把每条边的边权加上两顶点的点权作为新的边权,然后跑个最小生成树 ...

  6. Paris Traceroute

    Paris Traceroute is a new version of the well-known network diagnosis and measurement tool. Why shou ...

  7. 东大OJ-1391-Big big Power

    题目描述 Calculate the power num a^(b^c) mod 1e9+7 输入 Multiple test cases,each case has three integers a ...

  8. 42-stat 显示文件的信息

    显示文件的信息 stat [options] [file-list] 参数 file-list指定stat所显示的一个或多个文件的路径名 选项 -f                     显示文件系 ...

  9. display:block、display:inline与displayinline:block的概念和区别

    总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 block-level elements (块级元素) 和 inline elements (内联元素).block元素通 ...

  10. 基本数据类型-集合(set)_上周内容回顾(字符串_数字_列表_元组_字典_集合)

    上周内容回顾 1.字符串 2.数字 除了布尔类型外,int.long.float和complex都可以使用的运算为:加.减.乘.除.整除.幂运算和取余 3.列表和元组 列表的内容可变,可以包含任意对象 ...