【poj1733】 Parity game
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的更多相关文章
- 【POJ1733】Parity game
[POJ1733]Parity game 题面 vjudge 题解 比较简单的分类并查集 将一个查询操作看作前缀和\(s_r-s_{l-1}\)的奇偶性 将每个点拆成一奇一偶然后分别连边即可 如果一个 ...
- 【poj1733】Parity game--边带权并查集
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15776 Accepted: 5964 Description Now ...
- 【POJ1733】【带标记并查集】Parity game
Description Now and then you play the following game with your friend. Your friend writes down a seq ...
- 【Codeforces Global Round 1 A】Parity
[链接] 我是链接,点我呀:) [题意] 给你一个k位数b进制的进制转换. 让你求出来转成10进制之后这个数字是奇数还是偶数 [题解] 模拟一下转换的过程,加乘的时候都记得对2取余就好 [代码] im ...
- 【POJ 1733】 Parity Game
[题目链接] http://poj.org/problem?id=1 [算法] 并查集 [代码] #include <algorithm> #include <bitset> ...
- 【leetcode】905. Sort Array By Parity
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- 【C#】串口操作实用类
做工业通 信有很长时间了,特别是串口(232/485),有VB/VC/C各种版本的串口操作代码,这些代码也经过了多年的现场考验,应该说是比较健壮的代码,但 是目前却没有C#相对成熟的串口操作代码,最近 ...
随机推荐
- 假设检验:p-value,FDR,q-value
来源:http://blog.sina.com.cn/s/blog_6b1c9ed50101l02a.html,http://wenku.baidu.com/link?url=3mRTbARl0uPH ...
- HDU1281-棋盘游戏-二分图匹配
先跑一个二分图匹配,然后一一删去匹配上的边,看能不能达到最大匹配数,不能这条边就是重要边 /*----------------------------------------------------- ...
- PRML读书会第十四章 Combining Models(committees,Boosting,AdaBoost,决策树,条件混合模型)
主讲人 网神 (新浪微博: @豆角茄子麻酱凉面) 网神(66707180) 18:57:18 大家好,今天我们讲一下第14章combining models,这一章是联合模型,通过将多个模型以某种形式 ...
- 【python游戏编程之旅】第九篇---嗷大喵快跑小游戏开发实例
本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 前几期博客我们一起学习了,pygame中的冲突检测技术以及一些常用的数据结构. 这次我们来一起做一个简单的酷 ...
- 狗屎的Java规范
规范 标题真有点侮辱了狗. 最近做Java,C#与Java对比笔记: http://www.cnblogs.com/newsea/p/4839540.html 其实Java语法弱点就弱点,关键是Jav ...
- dinic模板
procedure addedge(u,v,cap:longint); begin sid[tot].u:=u; sid[tot].v:=v; sid[tot].cap:=cap; sid[tot]. ...
- 我的第一个jsp程序-实现注册登录留言功能
1,注册功能,包括两个页面 zhuce.jsp注册页面 <%@ page language="java" contentType="text/html; chars ...
- idea 新建web项目
- 【转】c# 调用windows API(user32.dll)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- C#的值参数与引用参数
值参数:在使用值参数时,是把变量的值传给函数,函数中对此变量的任何修改都不影响该变量本身的值. 引用参数:使用引用参数时,在函数中对此变量的修改会影响变量的值. 说简单点,值参数,就是我把身份证复印件 ...