#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. MongoDb在windows下的安装与以auth方式启用服务

    一.下载安装 1.去官网上下载适合自己电脑的MongoDB版本  下载MongoDB 2.安装MongoDB 安装还是比较简单,按照步骤一步一步往下走就可以了. 3.启动MongodDB 安装完成之后 ...

  2. 解决 重启nginx: [alert] kill(189, 1) failed (3: No such process)

    解决 nginx: [alert] kill(189, 1) failed (3: No such process) [root@localhost/]# nginx -s reloadnginx: ...

  3. Linux下C++/C的编译调试

    这几天因为任务的原因我需要在ubuntu下编写程序.因此恶补了许多linux程序编写的知识.我分以下几个方面总结我所学的知识. gcc,g++,make命令的使用 gdb 调试 VScode的使用 c ...

  4. ReentrantLock与Condition构造有界缓存队列与数据栈

    通过ReentrantLock与Condition的设计,以数组为基础,可以实现简单的队列和栈的数据结构,临界阻塞的效果. ReentrantLock相对于synchronized比较大的一个区别是有 ...

  5. UE4 分层材质 Layerd Materials

    在UE4中最正规的材质制作流程就像: 建立新材质,并将其调整为达至完美. 在内容浏览器中,建立新材质函数,并将所有材质函数节点复制/粘贴到其中. 将网络连接到新的 Make Material Attr ...

  6. jQuery:下拉列表的联动

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  7. [SinGuLaRiTy] NOIP模拟题 by liu_runda

    [SinGuLaRiTy-1046] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 题目名称 兔子 被子 蚊子 源程序文件名 rabbit. ...

  8. plist涉及到沙盒的一个问题

    http://blog.csdn.net/wowxavi1/article/details/8557271

  9. Django之Model组件

    Model组件在django基础篇就已经提到过了,本章介绍更多高级部分. 一.回顾 1.定义表(类) ##单表 from django.db import models class user(mode ...

  10. linux 磁盘加密和tpm搭配使用1

    一.基本名称,容易混淆 1.dm-crypt是linux的2.6内核开始集成的一种磁盘加密功能.十几年来,连sche调度算法都被改了N次,但dm-crypt一直稳定在内核中,稳定性还是很好的. 2.c ...