思路:d(i, j)表示区间(i, j]的1的个数的奇偶性。输入最多共有5000*2个点,需要离散化处理一下。剩下的就是并查集判冲突。

AC代码

#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 1e4 + 5;
int n, m;
struct node{
	int par;
	int real;
}a[maxn];

struct Edge{
	int u, v, k;
}b[maxn];

void init(int num) {
	for(int i = 0; i < num; ++i) {
		a[i].par = i;
		a[i].real = 0;
	}
}

int find(int x) {
	if(a[x].par == x) return x;
	int r = find(a[x].par);
	a[x].real = a[x].real ^ a[a[x].par].real;
	return a[x].par = r;
}

bool unionset(int x, int y, int real) {
	int rx = find(x), ry = find(y);
	if(rx == ry) {
		if(real != (a[x].real ^ a[y].real)) return false;
	}
	else {
		a[rx].par = y;
		a[rx].real = a[x].real ^ real;
	}
	return true;
}

void deal() { //离散化处理
	map<int, int>ha;
	vector<int>v;
	for(int i = 0; i < m; ++i) {
		if(!ha.count(b[i].u)) {
			ha[b[i].u] = 1;
			v.push_back(b[i].u);
		}
		if(!ha.count(b[i].v)) {
			ha[b[i].v] = 1;
			v.push_back(b[i].v);
		}
	}
	sort(v.begin(), v.end());
	int id = 0;
	for(int i = 0; i < v.size(); ++i) {
		ha[v[i]] = id++;
	}
	init(id);
	for(int i = 0; i < m; ++i) {
		b[i].u = ha[b[i].u];
		b[i].v = ha[b[i].v];
	}
} 

int main() {
	while(scanf("%d%d", &n, &m) == 2) {
		char s[10];
		for(int i = 0; i < m; ++i) {
			scanf("%d%d%s", &b[i].u, &b[i].v, s);
			b[i].u--;
			if(s[0] == 'o') b[i].k = 1; //奇数
			else b[i].k = 0;
		}
		deal();
		int ans = 0, flag = 1;
		for(int i = 0; i < m; ++i) {
			ans = i;
			if(!unionset(b[i].u, b[i].v, b[i].k)) {
				flag = 0;
				break;
			}
		}
		if(flag) ans = m;
		printf("%d\n", ans);
	}
	return 0;
} 

如有不当之处欢迎指出!

POJ - 1733 Parity game 种类并查集+离散化的更多相关文章

  1. POJ1733 Parity game —— 种类并查集

    题目链接:http://poj.org/problem?id=1733 Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  2. poj 1182:食物链(种类并查集,食物链问题)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44168   Accepted: 12878 Description ...

  3. poj 1182 食物链(种类并查集 ‘初心者’)

    题目链接:http://poj.org/problem?id=1182 借着这题可以好好理解一下种类并查集,这题比较简单但挺经典的. 题意就不解释了,中问题. 关于种类并查集结局方法也是挺多的 1扩增 ...

  4. poj1733(种类并查集+离散化)

    题目链接: http://poj.org/problem?id=1733 题意: 输入n表示有一个长度为n的0,1字符串, m表示接下来有m行输入, 接下来的m行输入中x, y, even表示第x到第 ...

  5. POJ 1182 食物链(种类并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63592   Accepted: 18670 Description ...

  6. POJ 1182 食物链 (种类并查集)

    动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种.有人用两种说 ...

  7. POJ 1417 True Liars(种类并查集+dp背包问题)

    题目大意: 一共有p1+p2个人,分成两组,一组p1,一组p2.给出N个条件,格式如下: x y yes表示x和y分到同一组,即同是好人或者同是坏人. x y no表示x和y分到不同组,一个为好人,一 ...

  8. POJ_1733 Parity game 【并查集+离散化】

    一.题面 POJ1733 二.分析 该题与之前做过的带权并查集的唯一区别就是数组开不下.所以需要用离散化的思想,只取那些有用的点来解决该问题. 离散化其实就是把这些所有用到的点收集后,去重,再排一下序 ...

  9. [POJ1733]Parity game(并查集 + 离散化)

    传送门 题意:有一个长度已知的01串,给出[l,r]这个区间中的1是奇数个还是偶数个,给出一系列语句问前几个是正确的 思路:如果我们知道[1,2][3,4][5,6]区间的信息,我们可以求出[1,6] ...

随机推荐

  1. HBuilder连接IOS手机打开APP测试

    HBuilder是专为前端打造的开发工具,具有最全的语法库和浏览器兼容数据,以方便制作手机APP,最保护眼睛的绿柔设计等优点在近几年盛行: 开发移动端APP项目想要在手机真机上测试: 首先打开HBui ...

  2. python_7_列表

    什么是列表? --一种数据类型 -- 形式:[值1,值2,[值a,值b],值3]      --可以嵌套 #!/usr/bin/python3 list_a = [1, 2, [3, 'a']] 对于 ...

  3. maven-assembly-plugin插件的使用方法

    一. Assembly 是什么意思? 二. maven-assembly-plugin是什么? 它是maven中针对打包任务而提供的标准插件. 三. maven-assembly-plugin插件的作 ...

  4. JDK自带的Timer类

    Timer类负责设定TimerTask的起始和间隔执行时间.具体的执行任务,由用户创建一个TimerTask的继承类,并实现其run()方法 timer.schedule()

  5. 豹哥嵌入式讲堂:ARM知识概要杂辑(1)- 内核架构编年史

    众所周知,ARM公司是一家微处理器行业的知名企业,ARM公司本身并不靠自有的设计来制造或出售CPU,而是将处理器架构授权给有兴趣的厂家.这些厂家基本涵盖了全球领先的知名半导体企业.软件和OEM厂商:T ...

  6. HTML核心标签之表格标签(二)

    基本用法: <ul type="cir"> <li>显示数据</li> <li>显示数据</li> </ul> ...

  7. mybatis-generator 根据表生成对应文件

    1 创建maven工程 2.编辑.pom文件 <?xml version="1.0" encoding="UTF-8"?> <project ...

  8. Hadoop RPC简单例子

    jdk中已经提供了一个RPC框架-RMI,但是该PRC框架过于重量级并且可控之处比较少,所以Hadoop RPC实现了自定义的PRC框架. 同其他RPC框架一样,Hadoop RPC分为四个部分: ( ...

  9. bind,apply,call区别总结

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  10. Selenium_WebDriver_控制浏览器

    版权声明:本文为博主原创文章,转载请注明出处.  浏览器环境: ①GoogleChrome:60 ②chromedriver:2.30: 访问 操作 方法 示例 访问页面 void get(java. ...