【POJ1733】Parity game

题面

vjudge

题解

比较简单的分类并查集

将一个查询操作看作前缀和\(s_r-s_{l-1}\)的奇偶性

将每个点拆成一奇一偶然后分别连边即可

如果一个点的奇点和偶点被连在一起了就判无解即可

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
using namespace std; inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (ch != '-' && (ch > '9' || ch < '0')) ch = getchar();
if (ch == '-') w = -1 , ch = getchar();
while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
return w * data;
}
const int MAX_N = 5005;
int M, N, l[MAX_N], r[MAX_N], X[MAX_N << 1], cnt;
int fa[MAX_N << 1];
int getf(int x) { return (x == fa[x]) ? x : (x = getf(fa[x])); }
void unite(int x, int y) { fa[getf(x)] = getf(y); }
bool same(int x, int y) { return getf(x) == getf(y); }
char ch[MAX_N][10];
int main () {
M = gi(), N = gi();
for (int i = 1; i <= N; i++) {
l[i] = gi() - 1, r[i] = gi();
X[++cnt] = l[i], X[++cnt] = r[i];
scanf("%s", ch[i]);
}
sort(&X[1], &X[cnt + 1]); cnt = unique(&X[1], &X[cnt + 1]) - X - 1;
for (int i = 1; i <= N; i++) {
l[i] = lower_bound(&X[1], &X[cnt + 1], l[i]) - X;
r[i] = lower_bound(&X[1], &X[cnt + 1], r[i]) - X;
}
for (int i = 1; i <= N * 2; i++) fa[i] = i;
for (int i = 1; i <= N; i++) {
char op = ch[i][0]; int x = l[i], y = r[i];
if (op == 'e') {
unite(x, y); unite(x + N, y + N);
if (same(x + N, x) || same(y, y + N)) return printf("%d\n", i - 1) & 0;
} else {
unite(x, y + N); unite(x + N, y);
if (same(x + N, x) || same(y, y + N)) return printf("%d\n", i - 1) & 0;
}
}
printf("%d\n", N);
return 0;
}

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

  1. 【poj1733】 Parity game

    http://poj.org/problem?id=1733 (题目链接) 题意 一个由0,1组成的序列,每次给出一段区间的奇偶,问哪一条信息不合法. Solution 并查集. 题目中序列的长度有很 ...

  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. mongodb启动和关闭

    mongodb的启动 mongod --dbpath=/data/mongodb/data --logpath=/data/mongodb/log/33988.log --port 33988 --f ...

  2. Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

    任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...

  3. Android大图片裁剪终极解决方案(下:拍照截图)

    http://blog.csdn.net/floodingfire/article/details/8144617 http://mzh3344258.blog.51cto.com/1823534/8 ...

  4. 前端面试题(copy)

    前端开发面试知识点大纲: HTML&CSS: 对Web标准的理解.浏览器内核差异.兼容性.hack.CSS基本功:布局.盒子模型.选择器优先级及使用.HTML5.CSS3.移动端适应. Jav ...

  5. TCP-IP and Advanced Topics 课程总结与报告

    课程总结 学习了四周十六课的课程,对每一课的知识点进行总结梳理,作出一个树状的知识网络图. 本课程虽然在深度上有所欠缺,但却更有利于结构上的梳理,加深总体上对网络的理解. 本课程从Internet出发 ...

  6. flask总结之session,websocket,上下文管理

    1.关于session flask是带有session的,它加密后存储在用户浏览器的cookie中,可以通过app.seesion_interface源码查看 from flask import Fl ...

  7. 禁用Linux透明大页

    Oracle 安装时官方建议关闭Linux的透明大页,防止内存延迟分配导致的性能问题 https://docs.oracle.com/cd/E11882_01/install.112/e47689/p ...

  8. 05 oracle中lock和latch的用途

    oracle中lock和latch的用途   本文向各位阐述Oracle的Latch机制,Latch,用金山词霸翻译是门插栓,闭锁,专业术语叫锁存器,我开始接触时就不大明白为什么不写Lock,不都是锁 ...

  9. mysql复制表结构和数据

    1.复制表结构: create table newName like oldName;//可以复制所有结构. 或者: create table newName select * from oldNam ...

  10. pastedeploy

    3.1作用 不修改WSGI应用程序的情况下通过配置文件配置WSGI服务. filter:过滤器,滤网. pipline:管道 app:application 应用,在这个语境下我举个例子吧,lavab ...