POJ - 1733 Parity game 种类并查集+离散化
思路: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 种类并查集+离散化的更多相关文章
- POJ1733 Parity game —— 种类并查集
题目链接:http://poj.org/problem?id=1733 Parity game Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- poj 1182:食物链(种类并查集,食物链问题)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44168 Accepted: 12878 Description ...
- poj 1182 食物链(种类并查集 ‘初心者’)
题目链接:http://poj.org/problem?id=1182 借着这题可以好好理解一下种类并查集,这题比较简单但挺经典的. 题意就不解释了,中问题. 关于种类并查集结局方法也是挺多的 1扩增 ...
- poj1733(种类并查集+离散化)
题目链接: http://poj.org/problem?id=1733 题意: 输入n表示有一个长度为n的0,1字符串, m表示接下来有m行输入, 接下来的m行输入中x, y, even表示第x到第 ...
- POJ 1182 食物链(种类并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63592 Accepted: 18670 Description ...
- POJ 1182 食物链 (种类并查集)
动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种.有人用两种说 ...
- POJ 1417 True Liars(种类并查集+dp背包问题)
题目大意: 一共有p1+p2个人,分成两组,一组p1,一组p2.给出N个条件,格式如下: x y yes表示x和y分到同一组,即同是好人或者同是坏人. x y no表示x和y分到不同组,一个为好人,一 ...
- POJ_1733 Parity game 【并查集+离散化】
一.题面 POJ1733 二.分析 该题与之前做过的带权并查集的唯一区别就是数组开不下.所以需要用离散化的思想,只取那些有用的点来解决该问题. 离散化其实就是把这些所有用到的点收集后,去重,再排一下序 ...
- [POJ1733]Parity game(并查集 + 离散化)
传送门 题意:有一个长度已知的01串,给出[l,r]这个区间中的1是奇数个还是偶数个,给出一系列语句问前几个是正确的 思路:如果我们知道[1,2][3,4][5,6]区间的信息,我们可以求出[1,6] ...
随机推荐
- Switch-case 内定义变量的问题
Switch-case 内定义变量的问题 这个问题需要分开讨论,C 语言和 C++ 的标准定义是不同的. C++ int Caset(int a) { switch (a) { case 1: int ...
- 数据仓库搭建——Inmon与Kimball
一.简介 1.1 历史 搞数据仓库这么久,实践中发现首先搭建数据集市,还是清洗数据之后,直接进入数据立方体(形成维度表和实施表)形成核心数据仓库层,是个选择题... 随后发现这其实涉及到了数据仓库的历 ...
- PHP与XML
代码: <?php $dom= new DomDocument('1.0'); $books=$dom->appendChild($dom->createElement_x_x('b ...
- Zabbix系统数据采集方法总结
转:http://www.blog.chinaunix.net/uid-9411004-id-4115731.html 老文章,直接拿来用了,官网也有最新分类,没高兴翻译 在Zabbix系统中有多达十 ...
- spring之构造注入
第一种:通过构造name和value属性(不常用) <!-- userAction --> <bean id="userAction" class="c ...
- cdh版本的hive安装以及配置
hive依赖hadoop 需要的软件包:hive-0.13.1-cdh5.3.6.tar.gz .hadoop-2.5.0-cdh5.3.6.tar.gz 1.hadoop的安装步骤请访问: http ...
- Log4Net记录到MySql
1.新建控制台程序. 2.添加Log4Net nuget 3.添加MySql 引用 4.添加配置文件如下: <?xml version="1.0"?> <conf ...
- Java程序只运行一个实例[转]
如果希望你的Java程序只能存在一个实例,可以参考下面的用法. 原文链接:http://blog.csdn.net/yaerfeng/article/details/7264729 Java没有提供这 ...
- POJ 2888 Magic Bracelet [Polya 矩阵乘法]
传送门 题意:竟然扯到哈利波特了.... 和上一题差不多,但颜色数很少,给出不能相邻的颜色对 可以相邻的连边建图矩阵乘法求回路个数就得到$f(i)$了.... 感觉这样的环上有限制问题挺套路的...旋 ...
- Python之CVXOPT模块
Python中支持Convex Optimization(凸规划)的模块为CVXOPT,其安装方式为: 卸载原Pyhon中的Numpy 安装CVXOPT的whl文件,链接为:https://www ...