【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. tp3.2中前台模板中日期时间的转换

    {$vo.create_time|date='Y-m-d',###} 其中###是占位符

  2. BZOJ 1878 [SDOI2009]HH的项链 【莫队】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=1878 1878: [SDOI2009]HH的项链 Time Limit: 4 Sec  M ...

  3. Html5的本地储存 Web Storage

      Html5 中的存储包括两种:sessionStorage 和 localStorage   sessionStorage 用于本地存储一个会话(session)中的数据,这些数据只有在同一个会话 ...

  4. MyBatis(2)增删改查

    本次全部学习内容:MyBatisLearning   查: 根据id查询用户信息,得到一个用户信息   在User.xml文件中添加代码: <mapper namespace="tes ...

  5. 第六章.MyBatis缓存结构

    一级缓存 测试案例: MyBatisTest.java //缓存 @Test public void testFindCustomerCache1() throws Exception{ SqlSes ...

  6. Python:文件的读取、创建、追加、删除、清空

    一.用Python创建一个新文件,内容是从0到9的整数, 每个数字占一行:#python>>>f=open('f.txt','w')    # r只读,w可写,a追加>> ...

  7. 01-Python学习笔记-基础语法

    Python标识符 -d           在解析时显示调试信息 -O           生成优化代码 ( .pyo 文件 ) -S           启动时不引入查找Python路径的位置 - ...

  8. kafka 参数配置 1

    kafka 参数配置 #参数配置 * broker.id : kafka 集群的唯一,标识每个broker * log.dirs : 指定kafka持久化消息的目录,可以设置多个目录,如:/home/ ...

  9. 类似QQ的聊天工程

    首先建立一个html:<!DOCTYPE html><html lang="en"><head> <meta charset=" ...

  10. oracle11g的安装与卸载

    1.首先确认电脑已安装 .NET Framework3.5 2.本地计算机的安装: 参考:https://www.cnblogs.com/hoobey/p/6010804.html 3.服务器的安装: ...