uoj207 共价大爷游长沙 子树信息 LCT + 随机化 + 路径覆盖
题目传送门
题解
如果是一棵静态的树,有一个非常容易想到的算法:统计一下目前的每一个条边被几条路径经过,如果 \(x\) 到 \(y\) 的边的这个值为 \(|S|\) 的话,那么就是合法的。
但是如果树是动态的,这个算法就有问题了。
link 和 cut 会导致一个点对之间的路径发生改变。
考虑到如果 \(x\) 到 \(y\) 这条边必须要被经过的话,那么就是说覆盖了 \(x\) 到 \(y\) 这条边的路径集恰好是 \(S\)。
回顾 bzoj3569 DZY Loves Chinese II 的做法,把一条路径用同一个 \(int\) 范围内随机的值在路径上的边都异或一边,那么如果两条边的值相同就意味着两条边很有可能是被相同的路径集覆盖。
所以这题也同理,但是因为没有根,所以不能像一般的树一样做树上差分——所以我们直接求子树异或和就可以了。
所以可用维护子树信息的 LCT 维护。
时间复杂度 \(O(m\log n)\)。
#include<bits/stdc++.h>
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back
template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
}
const int N = 100000 + 7;
const int M = 300000 + 7;
#define lc c[0]
#define rc c[1]
int n, m, cnt, yz;
int b[M];
pii a[M];
struct Node { int c[2], rev, fa, v, xsum, xs; } t[N];
int st[N];
inline bool idtfy(int o) { return t[t[o].fa].rc == o; }
inline bool isroot(int o) { return t[t[o].fa].lc != o && t[t[o].fa].rc != o; }
inline void connect(int fa, int o, int d) { t[fa].c[d] = o, t[o].fa = fa; }
inline void pushup(int o) { t[o].xsum = t[t[o].lc].xsum ^ t[t[o].rc].xsum ^ t[o].xs ^ t[o].v; }
inline void pushdown(int o) {
if (!t[o].rev) return;
if (t[o].lc) t[t[o].lc].rev ^= 1, std::swap(t[t[o].lc].lc, t[t[o].lc].rc);
if (t[o].rc) t[t[o].rc].rev ^= 1, std::swap(t[t[o].rc].lc, t[t[o].rc].rc);
t[o].rev = 0;
}
inline void rotate(int o) {
int fa = t[o].fa, pa = t[fa].fa, d1 = idtfy(o), d2 = idtfy(fa), b = t[o].c[d1 ^ 1];
if (!isroot(fa)) t[pa].c[d2] = o; t[o].fa = pa;
connect(o, fa, d1 ^ 1), connect(fa, b, d1);
pushup(fa), pushup(o);
}
inline void splay(int o) {
int x = o, tp = 0;
st[++tp] = x;
while (!isroot(x)) st[++tp] = x = t[x].fa;
while (tp) pushdown(st[tp--]);
while (!isroot(o)) {
int fa = t[o].fa;
if (isroot(fa)) rotate(o);
else if (idtfy(o) == idtfy(fa)) rotate(fa), rotate(o);
else rotate(o), rotate(o);
}
}
inline void access(int o) {
for (int x = 0; o; o = t[x = o].fa) {
splay(o);
t[o].xs ^= t[t[o].rc].xsum;
t[o].rc = x;
t[o].xs ^= t[t[o].rc].xsum;
pushup(o);
}
}
inline void mkrt(int o) {
access(o), splay(o);
t[o].rev ^= 1, std::swap(t[o].lc, t[o].rc);
}
inline int getrt(int o) {
access(o), splay(o);
while (pushdown(o), t[o].lc) o = t[o].lc;
return splay(o), o;
}
inline void link(int x, int y) {
mkrt(x);
if (getrt(y) != x) access(y), splay(y), t[x].fa = y, t[y].xs ^= t[x].xsum, pushup(y);
}
inline void cut(int x, int y) {
mkrt(x), access(y), splay(y);
if (t[y].lc == x && !t[x].rc) t[y].lc = t[x].fa = 0, pushup(y);
}
inline void work() {
while (m--) {
int opt, x, y, u, v;
read(opt);
if (opt == 1) {
read(x), read(y), read(u), read(v);
cut(x, y), link(u, v);
} else if (opt == 2) {
read(x), read(y);
v = rand(), yz ^= v;
access(x), splay(x), t[x].v ^= v, pushup(x);
access(y), splay(y), t[y].v ^= v, pushup(y);
a[++cnt] = pii(x, y), b[cnt] = v;
} else if (opt == 3) {
read(u);
x = a[u].fi, y = a[u].se, v = b[u], yz ^= v;
access(x), splay(x), t[x].v ^= v, pushup(x);
access(y), splay(y), t[y].v ^= v, pushup(y);
} else if (opt == 4) {
read(x), read(y);
mkrt(x), access(y), splay(x);
assert(t[x].rc == y && !t[y].lc);
// dbg("t[y].xsum = %d, yz = %d, %d, %d\n", t[y].xsum, yz, t[y].v, t[y].xs);
if (t[y].xsum != yz) puts("NO");
else puts("YES");
}
}
}
inline void init() {
srand(time(0) + (ull)new char);
read(n), read(n), read(m);
int x, y;
for (int i = 1; i < n; ++i) read(x), read(y), link(x, y);
}
int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}
uoj207 共价大爷游长沙 子树信息 LCT + 随机化 + 路径覆盖的更多相关文章
- 【LCT维护子树信息】uoj207 共价大爷游长沙
这道题思路方面就不多讲了,主要是通过这题学一下lct维护子树信息. lct某节点u的子树信息由其重链的一棵splay上信息和若干轻儿子子树信息合并而成. splay是有子树结构的,可以在rotate, ...
- [UOJ207]共价大爷游长沙
UOJ sol 这题真是太神啦! 对于S集合中的每个点对,给他们随机附上一个相同权值. 两个点在边(x,y)的两侧当且仅当一个点在x的子树中,另一个点不在x的子树中(假设x是y的儿子) 维护一下子树点 ...
- uoj207共价大爷游长沙
话说我可能还没有调出魔法森林呢...说好的lct第一题呢... 又是一个随机化的方法,毕竟又是判定性的问题 上次是判断无向图联通 这次是判断一些路径是否经过一条定边 若把路径上的边全部异或上一个路径的 ...
- UOJ #207. 共价大爷游长沙 [lct 异或]
#207. 共价大爷游长沙 题意:一棵树,支持加边删边,加入点对,删除点对,询问所有点对是否经过一条边 一开始一直想在边权上做文章,或者从连通分量角度考虑,比较接近正解了,但是没想到给点对分配权值所以 ...
- 【UOJ207】共价大爷游长沙(Link-Cut Tree,随机化)
[UOJ207]共价大爷游长沙(Link-Cut Tree,随机化) 题面 UOJ 题解 这题太神了 \(\%\%\%myy\) 看到动态的维护边很容易的想到了\(LCT\) 然后能否堵住一条路 我们 ...
- 「UOJ207」共价大爷游长沙
「UOJ207」共价大爷游长沙 解题思路 : 快速判断两个集合是否完全相等可以随机点权 \(\text{xor}\) 的思路可以用到这道题上面,给每一条路径随机一个点权,维护出经过每一条边的点权的 \ ...
- 【刷题】UOJ #207 共价大爷游长沙
火车司机出秦川,跳蚤国王下江南,共价大爷游长沙.每个周末,勤劳的共价大爷都会开车游历长沙市. 长沙市的交通线路可以抽象成为一个 \(n\) 个点 \(n−1\) 条边的无向图,点编号为 \(1\) 到 ...
- UOJ #207. 共价大爷游长沙
#207. 共价大爷游长沙 链接:http://uoj.ac/problem/207 题意:给一棵树,要求支持加边.删边.询问一条边是否被所有路径覆盖.同时路径端点集合有加入与删除操作. 想法: 考虑 ...
- 【UOJ#207】共价大爷游长沙
题目链接 题目描述 火车司机出秦川,跳蚤国王下江南,共价大爷游长沙.每个周末,勤劳的共价大爷都会开车游历长沙市. 长沙市的交通线路可以抽象成为一个 \(n\) 个点 \(n−1\) 条边的无向图,点编 ...
随机推荐
- post与get方法的区别
1. GET请求:请求的数据会附加在URL之后,以?分割URL和传输数据,多个参数用&连接.URL的编码格式采用的是ASCII编码,而不是unicode,即是说所有的非ASCII字符都要编码之 ...
- 十三、RF中对json的解析
A.需要安装的库 1.RequestsLibrary,安装命令:pip2 install requests 2.HttpLibrary,安装命令:pip2 install robotframework ...
- leetcode 437 Path Sum III 路径和
相关问题:112 path sum /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...
- Web - <a>标签中href="javascript:;"
javascript: 是一个伪协议,其他的伪协议还有 mail: tel: file: 等等. 1 <a id="jsPswEdit" class="set ...
- PostgreSQL 实现按月按年,按日统计 分组统计
endtime 是 timestamp select to_char(endtime, 'YYYY-MM-DD') as d , count(objectid) FROM sde.polygon wh ...
- ES6对象的拓展
属性的简洁表示法 ES6 允许直接写入变量和函数,作为对象的属性和方法.这样的书写更加简洁. const foo = 'bar'; const baz = {foo}; //允许直接写入变量和函数作为 ...
- 阶段3 1.Mybatis_06.使用Mybatis完成DAO层的开发_9 typeAliases标签和package标签
配置别名 上面制定了好了别名后,映射文件就可以简写了.不区分大小写 皆可以小写也可以大写 大小写混着也可以 测试 package 直接把com.itheima.domain下面所有的类都注册了 可以使 ...
- C语言的指针和数组
指针和内存 指针变量也是个变量,不过保存的是另一个变量的地址.另外编译器还会记住指针所指向变量的类型,从而在指针运算时根据变量类型采取不同操作. 例如,char * a 定义了char 类型的指针变量 ...
- mybatisProxy
config.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configurati ...
- Web UI自动化测试基础——元素定位(三)
本篇文章整理了元素定位的基础知识——iframe框架中的元素定位. 一.iframe框架元素定位 iframe是Html页面的内联框架,如果在自动化测试中无法定位到某个元素,那么很有可能是因为该元素在 ...