UOJ #207. 共价大爷游长沙 [lct 异或]
#207. 共价大爷游长沙
题意:一棵树,支持加边删边,加入点对,删除点对,询问所有点对是否经过一条边
一开始一直想在边权上做文章,或者从连通分量角度考虑,比较接近正解了,但是没想到给点对分配权值所以没做出来
题解的后两种做法说的很清楚了,我用了第二种因为我没写过lct维护子树信息
给点对分配权值后,我们只要看一条边的权值是否等于当前异或和就行了
加边删边时,把删除边\((u,v)\)的权值异或到之后\((u,v)\)的路径上,巧妙利用了异或的自反性,和wc那道xor很像
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
#define lc t[x].ch[0]
#define rc t[x].ch[1]
#define pa t[x].fa
const int N = 4e5+5;
inline int read(){
char c=getchar(); int x=0,f=1;
while(c<'0' || c>'9') {if(c=='-')f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x*f;
}
int n, m, type, x, y, u, v, tot, cnt, now;
map<int, int> eid[N];
struct pai{int x, y, val;} s[N];
namespace lct {
struct meow{ int ch[2], fa, rev, val, tag; } t[N];
inline int wh(int x) {return t[pa].ch[1] == x;}
inline bool isr(int x) {return t[pa].ch[0] != x && t[pa].ch[1] != x;}
inline void rever(int x) {t[x].rev ^= 1; swap(lc, rc);}
inline void paint(int x, int v) {t[x].tag ^= v; t[x].val ^= v;}
inline void pushdn(int x) {
if(t[x].rev) {
if(lc) rever(lc);
if(rc) rever(rc);
t[x].rev = 0;
}
if(t[x].tag) {
if(lc) paint(lc, t[x].tag);
if(rc) paint(rc, t[x].tag);
t[x].tag = 0;
}
}
inline void pd(int x) {if(!isr(x)) pd(pa); pushdn(x);}
inline void update(int x) {}
inline void rotate(int x) {
int f = t[x].fa, g = t[f].fa, c = wh(x);
if(!isr(f)) t[g].ch[wh(f)] = x; t[x].fa = g;
t[f].ch[c] = t[x].ch[c^1]; t[ t[f].ch[c] ].fa = f;
t[x].ch[c^1] = f; t[f].fa = x;
update(f); update(x);
}
inline void splay(int x) {
pd(x);
for(; !isr(x); rotate(x))
if(!isr(pa)) rotate(wh(x) == wh(pa) ? pa : x);
}
inline void access(int x) {
for(int y=0; x; y=x, x=pa)
splay(x), rc=y, update(x);
}
inline void maker(int x) { access(x); splay(x); rever(x);}
inline void link(int x, int y) { maker(x); t[x].fa = y; }
inline void cut(int x, int y) {
maker(x); access(y); splay(y);
t[x].fa = t[y].ch[0] = 0; update(y);
}
inline void split(int x, int y) { maker(x), access(y); splay(y); }
} using namespace lct;
void rep() {
x=read(); y=read(); if(x > y) swap(x, y);
int id = eid[x][y]; pd(id);
int val = t[id].val; //printf("val %d %d\n", id, val);
cut(id, x); cut(id, y);
int _x = x, _y = y;
x=read(); y=read(); if(x > y) swap(x, y);
eid[x][y] = ++cnt;
link(cnt, x); link(cnt, y);
split(_x, _y); paint(_y, val);
}
inline int ran() {return rand()+1;}
void add(int x, int y) {
s[++tot] = (pai){x, y, ran()};
split(x, y); paint(y, s[tot].val);
now ^= s[tot].val; //printf("add %d (%d, %d) %d %d\n", tot, x, y, s[tot].val, now);
}
void del(int id) {
int x = s[id].x, y = s[id].y;
split(x, y); paint(y, s[id].val);
now ^= s[id].val; //printf("del %d (%d, %d) %d %d\n", id, s[id].x, s[id].y, s[id].val, now);
}
void que(int x, int y) {
if(x > y) swap(x, y);
int id = eid[x][y]; pd(id); //printf("hi %d %d now %d\n", id, t[id].val, now);
puts(t[id].val == now ? "YES" : "NO");
}
int main() {
freopen("in", "r", stdin);
freopen("out", "w", stdout);
srand(317);
int qwq=read(); qwq++;
n=read(); m=read(); cnt=n;
for(int i=1; i<n; i++) {
u=read(), v=read();
if(u > v) swap(u, v);
eid[u][v] = ++cnt;
link(cnt, u); link(cnt, v);
}
for(int i=1; i<=m; i++) {
type=read();
if(type == 1) rep();
else if(type == 2) x=read(), y=read(), add(x, y);
else if(type == 3) x=read(), del(x);
else x=read(), y=read(), que(x, y);
}
}
UOJ #207. 共价大爷游长沙 [lct 异或]的更多相关文章
- [UOJ#207. 共价大爷游长沙]——LCT&随机化
题目大意: 传送门 给一颗动态树,给出一些路径并动态修改,每次询问一条边是否被所有路径覆盖. 题解: 先%一发myy. 开始感觉不是很可做的样子,发现子树信息无论维护什么都不太对…… 然后打开题目标签 ...
- UOJ#207. 共价大爷游长沙 LCT
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ207.html 题解 第一次听说 LCT 还可以维护子树信息. 首先对于每一条路径 rand 一个值,分别 ...
- UOJ #207. 共价大爷游长沙
#207. 共价大爷游长沙 链接:http://uoj.ac/problem/207 题意:给一棵树,要求支持加边.删边.询问一条边是否被所有路径覆盖.同时路径端点集合有加入与删除操作. 想法: 考虑 ...
- 【刷题】UOJ #207 共价大爷游长沙
火车司机出秦川,跳蚤国王下江南,共价大爷游长沙.每个周末,勤劳的共价大爷都会开车游历长沙市. 长沙市的交通线路可以抽象成为一个 \(n\) 个点 \(n−1\) 条边的无向图,点编号为 \(1\) 到 ...
- UOJ #207. 共价大爷游长沙(LCT + 异或哈希)
题目 维护一颗动态树,并维护一个点对集合 \(S\) . 动态查询一条边,是否被集合中所有点对构成的路径包含. \(n \le 100000, m \le 300000\) 题解 orz 前辈 毛爷爷 ...
- 数据结构(动态树):UOJ 207 共价大爷游长沙
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABHwAAAJZCAIAAABUW7XHAAAgAElEQVR4nOy93cstx5Xm2f9TXh2EOe
- 共价大爷游长沙 lct 维护子树信息
这个题目的关键就是判断 大爷所有可能会走的路 会不会经过询问的边. 某一条路径经过其中的一条边, 那么2个端点是在这条边的2测的. 现在我们要判断所有的路径是不是都经过 u -> v 我们以u为 ...
- 【UOJ#207】共价大爷游长沙
题目链接 题目描述 火车司机出秦川,跳蚤国王下江南,共价大爷游长沙.每个周末,勤劳的共价大爷都会开车游历长沙市. 长沙市的交通线路可以抽象成为一个 \(n\) 个点 \(n−1\) 条边的无向图,点编 ...
- 【UOJ207】共价大爷游长沙(Link-Cut Tree,随机化)
[UOJ207]共价大爷游长沙(Link-Cut Tree,随机化) 题面 UOJ 题解 这题太神了 \(\%\%\%myy\) 看到动态的维护边很容易的想到了\(LCT\) 然后能否堵住一条路 我们 ...
随机推荐
- GSS4 - Can you answer these queries IV(线段树懒操作)
GSS4 - Can you answer these queries IV(线段树懒操作) 标签: 线段树 题目链接 Description recursion有一个正整数序列a[n].现在recu ...
- HDU--1060
Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 互联网App应用程序测试流程及测试总结
互联网App应用程序测试流程及测试总结 1. APP测试基本流程 1.1流程图 仍然为测试环境 Pass 1.2测试周期 测试周期可按项目的开发周期来确定测试时间,一般测试时间为两三周(即15个工作日 ...
- [国嵌笔记][025][ARM指令分类学习]
算术和逻辑指令 1.mov 格式:mov {条件}{s} <dest>, <op> 作用:把一个值从一个地方移动到另一个地方,<dest>必须是寄存器 示例: @m ...
- 在form里面,放了四个UEditor,怎么在后台分别获取它们值
1) 默认情况下提交到后台的表单名称是 "editorValue",在editor_config.js中可以配置,参数名为textarea. 2) 可以在容器标签(即script标 ...
- APACHE服务器出现No input file specified.的完美解决方案
转自:http://www.upupw.net/server/n53.html 启用REWRITE的伪静态功能的时候,首页可以访问,而访问内页的时候,就提示:"No input file s ...
- ftp上传文件,本地安装了,服务器上也需要在也安装一个ftp
服务器需要配置FTP服务: 你说的在你自己电脑上安装的只是一个FTP软件,用于连接远程服务器进行上传和下载文件的. 追问 在本地已经安装了,链接的话要在服务器上也安装一个吗? 追答 额,你有FTP服务 ...
- ios知识点总结——UITableView的展开与收缩及横向Table
UITableVIew是iOS开发中使用最为广泛的一种控件,对于UITableView的基本用法本文不做探讨,本文主要是针对UITableView的展开与收缩进行阐述,在文章的后面也会探讨一下横向ta ...
- different between<A Href> and <jsp: forward>
i want to access Servelt by hyperlink in a jsp web site connection.jsp <%@ page contentType=" ...
- P2P视频模块
P2P视频模块数据手册 公 司 : 深圳市万秀电子有限公司 网 站 : http://www.wanxiucx.com 总 机 : 0755-23215689 联系人: 张先生 手 机 : 1 ...