【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. codeforces 814D An overnight dance in discotheque

    题目链接 正解:贪心. 首先我们可以计算出每个圆被多少个圆覆盖. 很显然,最外面的圆是肯定要加上的. 然后第二层的圆也是要加上的.那么第三层就不可能被加上了.同理,第四层的圆又一定会被加上. 然后我们 ...

  2. 问题解决:java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date

    问题描述: 数据表中有记录的time字段(属性为timestamp)其值为:“0000-00-00 00:00:00” 程序使用select 语句从中取数据时出现以下异常: Java.sql.SQLE ...

  3. The transaction log for database 'XXX' is full due to 'ACTIVE_TRANSACTION'.

    Msg 9002, Level 17, State 4, Line 4The transaction log for database 'Test' is full due to 'ACTIVE_TR ...

  4. POJ 3264 Balanced Lineup 【ST表 静态RMQ】

    传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total S ...

  5. 二十四、详述 IntelliJ IDEA 中自动生成 serialVersionUID 的方法

    当我们用 IntelliJ IDEA 编写类并实现 Serializable(序列化)接口的时候,可能会遇到这样一个问题,那就是: 无法自动生成serialVersionUID. 而serialVer ...

  6. 美化浏览器JSON格式

    工具下载地址: https://github.com/weibanggang/JSON-handle 原始 优化后 直接将文件拖到浏览器即可

  7. Excel批量导入商品,遇到导入失败记录到另一个Excel中供下载查看

    /// <summary> /// EXCEL批量导入 /// </summary> /// <param name="filePath">文件 ...

  8. Web项目开发中常见安全问题及防范

    计算机程序主要就是输入数据 经过处理之后 输出结果,安全问题由此产生,凡是有输入的地方都可能带来安全风险.根据输入的数据类型,Web应用主要有数值型.字符型.文件型. 要消除风险就要对输入的数据进行检 ...

  9. 『ACM C++』 Codeforces | 1066A - Points in Segments

    大一生活真 特么 ”丰富多彩“ ,多彩到我要忙到哭泣,身为班长,很多班级的事情需要管理,也是,什么东西都得体验学一学,从学生会主席.团委团总支.社团社长都体验过一番了,现在差个班长也没试过,就来体验了 ...

  10. chromium之message_pump_win之三

    上一篇分析MessagePumpForUI,参考chromium之message_pump_win之二 MessagePumpForIO,同MessagePumpForUI,也是要实现三个函数 // ...