【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;
}

最新文章

  1. 小白的CSS基础学习
  2. Asp.net Mvc 多级控制器 路由重写 及 多级Views目录 的寻找视图的规则 (多级路由) 如:Admin/Test/Index
  3. PHP与MySQL
  4. IOS - 多态
  5. jQuery - 2.jQuery选择器
  6. object.assign()方法的使用
  7. SAP 打开账期
  8. Binary Tree Level Order Traversal II
  9. 无法读取配置节“protocolMapping”,因为它缺少节声明
  10. 一个发光的搜索边框(纯CSS3)
  11. PCA和白化练习之处理图像
  12. Spring EL ternary operator (if-then-else) example
  13. java 引用资源-ClassLoader.getResource()方法
  14. centos 64位编译安装 glibc-2.14
  15. (转)python中的*args和**kw到底是个啥。看下面的例子就会懂了
  16. 弹性盒模型 flex box
  17. BPF漫谈
  18. maven项目pom.xml配置文件依赖
  19. loadrunner 参数存储在data.ws、paralist、globals.h 中区别(参数与变量额区别于使用)
  20. django-表单

热门文章

  1. sort给文件按照大小排序
  2. 鲁宾斯坦说:&quot;思维是在概括中完成的。&quot;
  3. 人类主动探索地外文明(METI)活动正在进行中
  4. VS 2013 scanf 报错问题
  5. Linux系统如何禁止普通用户切换root?
  6. phpstorm下TODO注释
  7. qt+vs2005新建配置不自动加载Generated Files进工程(个人备份)
  8. Reading Notes : 180215 计算机系统
  9. Oracle中case的第二种用法
  10. plsql中特殊字符的处理