Poj1733 Parity Game(带权并查集)
题面
题解
反正只要你判断是否满足区间的奇偶性,假设每一位要么是$1$要么是$0$好了。
假设有$S$的前缀和为$sum[]$,则有:
若$S[l...r]$中有奇数个$1$,则$sum[l-1]$与$sum[r]$不同奇偶;反之,则同奇偶
用一个带权并查集维护,设权值数组$s[i]$表示区间$[root[i]...i]$的和的奇偶性。
对于一个区间$[l,r]$,分情况讨论:
如果$root[l]=root[r]$,直接判断就行了。
否则的话,计算一下$s[fv]=t\oplus s[u]\oplus s[v]$
但是$n$太大了,需要离散化(懒人离散化用$map$,美滋滋)
#include <map>
#include <cstdio>
#include <cstring>
using std::map;
typedef long long ll;
const ll N = 1e4 + 10, Q = 5e3 + 10;
ll n, q, s[N], f[N], cnt;
map <ll, ll> m;
ll find (ll x) {
if(f[x] == -1) return x;
ll tmp = find(f[x]);
s[x] ^= s[f[x]];
return f[x] = tmp;
}
ll insert (ll x) {
if (m.find(x) == m.end()) m[x] = ++cnt;
return m[x];
}
int main() {
while (scanf ("%lld%lld", &n, &q) != EOF) {
char str[4];
memset(f, -1, sizeof f);
memset(s, 0, sizeof s);
ll ans = q;
for (register ll i = 1, u, v; i <= q; ++i) {
scanf ("%lld%lld%s", &u, &v, str);
if (ans < q) continue;
u = insert(u - 1), v = insert(v);
ll fu = find(u), fv = find(v);
ll t = 0;
if (str[0] == 'o') ++t;
if (fu == fv) { //root相等
if (s[u] ^ s[v] != t)
ans = i - 1;
} else { //一定有fu<fv(编号比较)
f[fv] = fu;
s[fv] = s[u] ^ s[v] ^ t;
//矢量的异或运算
}
}
printf ("%lld\n", ans);
}
return 0;
}
Poj1733 Parity Game(带权并查集)的更多相关文章
- poj1733 Parity game[带权并查集or扩展域]
地址 连通性判定问题.(具体参考lyd并查集专题该题的转化方法,反正我菜我没想出来).转化后就是一个经典的并查集问题了. 带权:要求两点奇偶性不同,即连边权为1,否则为0,压缩路径时不断异或,可以通过 ...
- URAL - 1003:Parity (带权并查集&2-sat)
Now and then you play the following game with your friend. Your friend writes down a sequence consis ...
- POJ1733 Party game [带权并查集or扩展域并查集]
题目传送 Parity game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10870 Accepted: 4182 ...
- POJ 1773 Parity game 带权并查集
分析:带权并查集,就是维护一堆关系 然后就是带权并查集的三步 1:首先确定权值数组,sum[i]代表父节点到子节点之间的1的个数(当然路径压缩后代表到根节点的个数) 1代表是奇数个,0代表偶数个 2: ...
- POJ 1733 Parity game (带权并查集)
题意:有序列A[1..N],其元素值为0或1.有M条信息,每条信息表示区间[L,R]中1的个数为偶数或奇数个,但是可能有错误的信息.求最多满足前多少条信息. 分析:区间统计的带权并查集,只是本题中路径 ...
- 【poj1733】Parity game--边带权并查集
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15776 Accepted: 5964 Description Now ...
- POJ1733 Parity game 【带权并查集】*
POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...
- POJ1733:Parity Game(离散化+带权并查集)
Parity Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12853 Accepted: 4957 题目链接 ...
- POJ-1733 Parity game(带权并查集区间合并)
http://poj.org/problem?id=1733 题目描述 你和你的朋友玩一个游戏.你的朋友写下来一连串的0或者1.你选择一个连续的子序列然后问他,这个子序列包含1的个数是奇数还是偶数.你 ...
随机推荐
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 经典题
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- 【转】 GRASP(通用职责分配软件模式)模式
转自:http://www.cnblogs.com/sevenyuan/archive/2010/03/05/1678730.html 及:http://blog.csdn.net/lovelion ...
- aio 爬虫,去重,入库
#aio 爬虫,去重,入库 import asyncio import aiohttp import aiomysql import re from pyquery import PyQuery st ...
- Spring Web 项目Junit测试报错问题
测试对象是Web项目的Service类,参照网上查到的资料,按如下方式执行时报错, //使用junit4进行单元测试 @RunWith(SpringJUnit4ClassRunner.class) / ...
- Jmeter-Java heap内存溢出
使用jmeter进行压力测试时遇到一段时间后报内存溢出outfmenmory错误,导致jmeter卡死了,先尝试在jmeter.bat中增加了JVM_ARGS="-Xmx2048m -Xms ...
- python学习笔记(六)之操作符
python中算术操作符: + - * / % ** // 注意: /:为真实除法,即对应数学中的除法,通常返回一个浮点数 //:取整除法,即取商 %:求模,即取余数 **:幂运算,这里需要注意的一点 ...
- POJ 2456 Aggressive cows ( 二分搜索)
题目链接 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The ...
- 使用idea+Tomcat搭建servlet服务器
1.使用java 搭建一个简单的Servlet 服务器 https://blog.csdn.net/qq_35164169/article/details/76146655
- arping详解
arping干嘛用的? arping主要干的活就是查看ip的MAC地址及IP占用的问题. 参数 -0:指定源地址为0.0.0.0,这个一般是在我们刚刚安装好系统,电脑还没配置好IP的时候 -a:Aud ...
- 用tkinter实现的gui小工具
import tkinter import requests import json from tkinter import * class FindLocation(object): def __i ...