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] ...
随机推荐
- Servlet--ServletRequest接口,ServletResponse接口
ServletRequest接口 定义 public interface ServletRequest 定义一个 Servlet 引擎产生的对象,通过这个对象, Servlet 可以获得客户端请求的数 ...
- [Python]Conda 介绍及常用命令
一.介绍 Anaconda 是一个用于科学计算的 Python 发行版,支持 Linux, Mac, Windows, 包含了众多流行的科学计算.数据分析的 Python 包.其使用conda系统进行 ...
- Hibernate (二)
1 一级缓存的引入 示例: @Test public void testUpdate(){ Configuration configuration = new Configuration().conf ...
- 溢出问题:overflow后最后一排文字显示不全
溢出字体会显示在padding区域(http://www.divcss5.com/css-hack/c680.shtml) section#xinwen li a h3{ border-bottom: ...
- zabbix监控windows agent安装配置
下载Windows的zabbix客户端 下载地址:http://www.zabbix.com/download.php 从官方下载Zabbix Agent后,压缩包里面有2个目录,bin和conf,c ...
- 101490E Charles in Charge
题目连接 http://codeforces.com/gym/101490 题目大意 你有一张图,每两点之间有一定距离,计算出比最短路大x%之内的路径中最长边的最小值 分析 先跑一遍最短路,然后二分答 ...
- oracle pl/sql如何定义变量
目的:如何在pl/sql中定义基本类型,引用类型,记录型变量? 以下plsql程序用的scott用户的dept,emp表. 定义基本类型的变量 set serveroutput on ; --使用基本 ...
- #pragma once 与 #ifndef 解析
转自:http://www.cnblogs.com/hokyhu/archive/2009/03/30/1425604.html 为了避免同一个文件被include多次,C/C++中有两种方式,一种是 ...
- MVVM之旅(1)创建一个最简单的MVVM程序
这是MVVM之旅系列文章的第一篇,许多文章和书喜欢在开篇介绍某种技术的诞生背景和意义,但是我觉得对于程序员来说,一个能直接运行起来的程序或许能够更直观的让他们了解这种技术.在这篇文章里,我将带领大家一 ...
- 开源中文分词工具探析(五):Stanford CoreNLP
CoreNLP是由斯坦福大学开源的一套Java NLP工具,提供诸如:词性标注(part-of-speech (POS) tagger).命名实体识别(named entity recognizer ...