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. 内网机(无网络安装 .NET Core win开发环境

    1.安装 vs2015 update3 2.按顺序安装以下包 DotNetCore.1.0.0-SDK.Preview2-x64.exe aspnetcoremodule_x64_en_rc2_14. ...

  2. Beta版本项目展示要求

    项目评审的定在1月5日上午9:00在新主楼D225进行. 在Beta阶段项目评审会上, 每个团队有12分钟展示时间,10分钟问答和机动时间,我们的展示也不需要PPT,大家把要展现的东西写成博客(可以有 ...

  3. swift邮箱手机验证

    import UIKit class Validate: NSObject { //邮箱.手机验证 enum ValidatedType { case Email case PhoneNumber } ...

  4. Jenkins进阶系列之——18Jenkins语言本地化

    在Jenkins中,英语一大片,看着各种蛋疼.非常高兴的是,Jenkins作为一个主流流行的持续构建工具,提供了一个本地化语言的配置界面. 你可以找到它,在Jenkins每页的左下角.如下图: 点击帮 ...

  5. Toxy新手指南

    Neuzilla出品 官方网站:http://toxy.codeplex.com QQ群:297128022 官方微信公众号: Toxy 是干嘛用的?它是.NET平台上的文件抽取框架,主要解决各种格式 ...

  6. github开源:企业级应用快速开发框架CIIP WEB+WIN+移动端

    简介 CIIP是基于XAF开发的开源信息系统框架.CIIP最常见的应用场景是基于数据库的企业级应用程序,例如供应链系统,ERP系统,MRP系统,CRM系统等. CIIP支持WEB版本.Windows桌 ...

  7. 求height数组

    procedure getheight; var i,po1,po2:longint; begin to len do begin ; po1:=i;po2:=sa[rank[i]-]; while ...

  8. C语言函数sscanf()的用法

    从文件读取数据是一件很麻烦的事,所幸有sscanf()函数. C语言函数sscanf()的用法 sscanf() - 从一个字符串中读进与指定格式相符的数据. 函数原型: int sscanf( st ...

  9. linux初级,连网

    第一次写,今天刚好装上centos,然后就玩装一些软件,但要联网,所以琢磨了半天连网,下面总结下今天积累的一些知识点吧! 首先需要查看电脑有没有装驱动:ifconfig -a, 看有没有eh0(网卡) ...

  10. C#分割字符串

    命名空间:System.String.Split 程序集:mscorlib( mscorlib.dll) 简单实例: string before = "12,50,30"; str ...