思路: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. python_语法糖_装饰器

    什么是高阶函数? -- 把函数名当做参数传给另外一个函数,在另外一个函数中通过参数调用执行 #!/usr/bin/python3 __author__ = 'beimenchuixue' __blog ...

  2. Oracle Sqlload 导入数据

    sqlload导入数据具有快,简单,无需校验等方便,多说无益 1 首先,oracle数据库要有这么个表,用来接收数据.我这里这个uuid是序列生成的,当然也可以sqlload导入时候分配uuid -- ...

  3. Java多线程之线程的同步

    Java多线程之线程的同步 实际开发中我们也经常提到说线程安全问题,那么什么是线程安全问题呢? 线程不安全就是说在多线程编程中出现了错误情况,由于系统的线程调度具有一定的随机性,当使用多个线程来访问同 ...

  4. 请求服务(RequestService)

    一个module中的web组件,负责将Service的结果按照适当的规范输出给前端.格式:http://server/moduleID/param0/param1/paramN/p.TYPE格式上包含 ...

  5. tcpdump 使用

    例子: 首先切换到root用户 tcpdump -w  aaa.cap   -i eth7   -nn -x  'port  9999'  -c  1 以例子说明参数: -w:输出到文件aaa.cap ...

  6. echo 0000

    一个奇怪的问题,正常状态下如果sql插入失败,则输出0000,代码如下: $stmt=$db->prepare("insert into message(user,title,cont ...

  7. HTML5与css3权威指南(一)

    doctype声明: <!DOCTYPE html> 字符编码: <meta charset="utf-8"> 不允许写结束标记:area,base,br. ...

  8. matlab判断文件或文件夹是否存在

    当前目录中包含以下文件及文件夹: startup.m win64/ … 判断当前目录中是否存在startup.m文件 if ~exist('startup.m','file')==0    error ...

  9. 2018-01-05-医药行业的IT革命探讨

    layout: post title: 2018-01-05-医药行业的IT革命探讨 key: 20180105 tags: IT AI 医疗 modify_date: 2018-01-05 --- ...

  10. use zlib lib to compress or decompress file

    If you want to compress or decompress file when writing C++ code,you can choose zlib library,that's ...