#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 异或]的更多相关文章

  1. [UOJ#207. 共价大爷游长沙]——LCT&随机化

    题目大意: 传送门 给一颗动态树,给出一些路径并动态修改,每次询问一条边是否被所有路径覆盖. 题解: 先%一发myy. 开始感觉不是很可做的样子,发现子树信息无论维护什么都不太对…… 然后打开题目标签 ...

  2. UOJ#207. 共价大爷游长沙 LCT

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ207.html 题解 第一次听说 LCT 还可以维护子树信息. 首先对于每一条路径 rand 一个值,分别 ...

  3. UOJ #207. 共价大爷游长沙

    #207. 共价大爷游长沙 链接:http://uoj.ac/problem/207 题意:给一棵树,要求支持加边.删边.询问一条边是否被所有路径覆盖.同时路径端点集合有加入与删除操作. 想法: 考虑 ...

  4. 【刷题】UOJ #207 共价大爷游长沙

    火车司机出秦川,跳蚤国王下江南,共价大爷游长沙.每个周末,勤劳的共价大爷都会开车游历长沙市. 长沙市的交通线路可以抽象成为一个 \(n\) 个点 \(n−1\) 条边的无向图,点编号为 \(1\) 到 ...

  5. UOJ #207. 共价大爷游长沙(LCT + 异或哈希)

    题目 维护一颗动态树,并维护一个点对集合 \(S\) . 动态查询一条边,是否被集合中所有点对构成的路径包含. \(n \le 100000, m \le 300000\) 题解 orz 前辈 毛爷爷 ...

  6. 数据结构(动态树):UOJ 207 共价大爷游长沙

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABHwAAAJZCAIAAABUW7XHAAAgAElEQVR4nOy93cstx5Xm2f9TXh2EOe

  7. 共价大爷游长沙 lct 维护子树信息

    这个题目的关键就是判断 大爷所有可能会走的路 会不会经过询问的边. 某一条路径经过其中的一条边, 那么2个端点是在这条边的2测的. 现在我们要判断所有的路径是不是都经过 u -> v 我们以u为 ...

  8. 【UOJ#207】共价大爷游长沙

    题目链接 题目描述 火车司机出秦川,跳蚤国王下江南,共价大爷游长沙.每个周末,勤劳的共价大爷都会开车游历长沙市. 长沙市的交通线路可以抽象成为一个 \(n\) 个点 \(n−1\) 条边的无向图,点编 ...

  9. 【UOJ207】共价大爷游长沙(Link-Cut Tree,随机化)

    [UOJ207]共价大爷游长沙(Link-Cut Tree,随机化) 题面 UOJ 题解 这题太神了 \(\%\%\%myy\) 看到动态的维护边很容易的想到了\(LCT\) 然后能否堵住一条路 我们 ...

随机推荐

  1. BZOJ1294: [SCOI2009]围豆豆Bean

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1294 状压dp,dis[s][i][j]表示从(i,j)出发围的状态是s的最短路. 然后判断一 ...

  2. hdu_1037(水题水疯了。。。史上最水)

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int ma ...

  3. [国嵌笔记][013][Mini2440开发板介绍]

    系统资源 处理器:三星 S3C2440A ARM9 内存:64M SDRAM Nor Flash:2MB Nand  Flash:256MB LCD:3.5寸 分辨率320*240 启动模式 从nan ...

  4. UE4 UnLoadStreamLevel

    今天测试发现一个bug记录一下,如果把某一个子关卡的加载方式设置为Always Loaded时,调用UnloadStreamLevel时不会执行Completed后面的节点,也就是一直没有完成.

  5. keepalived VS zookeeper

    转载请标明出处http://www.cnblogs.com/haozhengfei/p/e3db73cb83afb213a3bff43a850d56c4.html keepalived VS zook ...

  6. GitHub上传文件或项目的教程

    既然是往GitHub上传文件,那GitHub账号必须得有,这时候就会有同学问:妖怪吧,我没有GitHub账号怎么办? 别急别急,打开GitHub网站https://github.com/,然后注册就O ...

  7. nodeJS里面的模块

    this 打开cmd,执行如下命令 nodeconsole.log(this); 输出如上信息,表示this是global,每个电脑的配置信息不一样的话,可能会有所差别的. 然后新建一个文件,写下如下 ...

  8. jsp页面固定页面为绝对路径

    1 <!-- 固定到绝对路径 --> 2 <base href="<%=request.getContextPath()%>/"/>

  9. php短信接口代码

    这篇文章主要为大家详细介绍了php短信接口代码,php短信发送.php批量发送.php获取余额等代码,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了几个常用的php短信接口代码,供大家参考,具体内 ...

  10. vue-cli的webpack模版项目配置解析-build/dev-server.js

    我们在使用vue-cli搭建vuejs项目(Vuejs实例-01使用vue-cli脚手架搭建Vue.js项目)的时候,会自动生成一系列文件,其中就包含webpack配置文件.我们现在来看下,这些配置到 ...