loj#121.「离线可过」动态图连通性
题面
话说#122怎么做啊
题解
我的\(\mathrm{LCT}\)水平极差,连最小生成树都快忘了,赶紧复习一下
做法和这篇是一样的
这道题还可以练习线段树分治
还可以练习ETT
果然是道吼题
代码
LCT
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<map>
#define RG register
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define clear(x, y) memset(x, y, sizeof(x))
inline int read()
{
int data = 0, w = 1; char ch = getchar();
while(ch != '-' && (!isdigit(ch))) ch = getchar();
if(ch == '-') w = -1, ch = getchar();
while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
}
const int maxn(505010), INF(0x3f3f3f3f);
struct edge { int opt, from, to, beg, end; } e[maxn];
int n, m, val[maxn], fa[maxn], son[2][maxn];
int stk[maxn], top, del[maxn], cnt, tim;
bool rev[maxn]; std::map<int, int> G[maxn];
inline bool isroot(int x) { return son[0][fa[x]] != x && son[1][fa[x]] != x; }
inline int getson(int x) { return son[1][fa[x]] == x; }
inline void update(int x)
{
del[x] = x;
if(val[del[x]] > val[del[son[0][x]]]) del[x] = del[son[0][x]];
if(val[del[x]] > val[del[son[1][x]]]) del[x] = del[son[1][x]];
}
inline void pushdown(int x)
{
if(!rev[x]) return;
rev[son[0][x]] ^= 1;
rev[son[1][x]] ^= 1;
rev[x] = 0, std::swap(son[0][x], son[1][x]);
}
inline void rotate(int x)
{
int f = fa[x], g = fa[f], l = getson(x), r = l ^ 1;
if(!isroot(f)) son[getson(f)][g] = x;
fa[x] = g, fa[f] = x, fa[son[r][x]] = f;
son[l][f] = son[r][x], son[r][x] = f;
update(f); update(x);
}
void splay(int x)
{
stk[top = 1] = x;
for(RG int i = x; !isroot(i); i = fa[i]) stk[++top] = fa[i];
for(RG int i = top; i; i--) pushdown(stk[i]);
for(; !isroot(x); rotate(x)) if(!isroot(fa[x]))
rotate(getson(fa[x]) ^ getson(x) ? x : fa[x]);
}
void access(int x)
{
for(RG int t = 0; x; t = x, x = fa[x])
splay(x), son[1][x] = t, update(x);
}
int find(int x) { access(x); splay(x); while(son[0][x]) x = son[0][x]; return x; }
inline void makeroot(int x) { access(x); splay(x); rev[x] ^= 1; }
inline void split(int x, int y) { makeroot(x); access(y); splay(y); }
inline void link(int x, int y) { makeroot(x); fa[x] = y; }
inline void cut(int x, int y) { split(x, y); son[0][y] = fa[x] = 0; update(y); }
int main()
{
cnt = n = read(), m = read();
for(RG int i = 1; i <= m; i++)
{
int opt = read(), x = read(), y = read();
if(x > y) std::swap(x, y);
++tim;
if(opt == 0) e[G[x][y] = tim].end = INF;
if(opt == 1) e[G[x][y]].end = tim;
e[tim].beg = tim, e[tim].opt = opt, e[tim].from = x, e[tim].to = y;
}
for(RG int i = 0; i <= cnt; i++) val[i] = INF;
for(RG int i = 1; i <= tim; i++)
{
int x1 = e[i].from, x2 = e[i].to;
if(e[i].opt == 0)
{
if(x1 == x2) continue;
if(find(x1) == find(x2))
{
split(x1, x2); int d = del[x2];
if(val[d] >= e[i].end) continue;
else cut(x1, d), cut(x2, d);
val[++cnt] = e[i].end;
link(x1, cnt), link(x2, cnt);
}
else val[++cnt] = e[i].end, link(x1, cnt), link(x2, cnt);
}
if(e[i].opt == 1)
{
if(find(x1) == find(x2))
{
split(x1, x2); int d = del[x2];
if(val[d] > e[i].beg) continue;
cut(x1, d), cut(x2, d);
}
}
if(e[i].opt == 2) puts(find(x1) == find(x2) ? "Y" : "N");
}
return 0;
}
线段树分治
咕咕咕
loj#121.「离线可过」动态图连通性的更多相关文章
- LOJ 121 「离线可过」动态图连通性——LCT维护删除时间最大生成树 / 线段树分治
题目:https://loj.ac/problem/121 离线,LCT维护删除时间最大生成树即可.注意没有被删的边的删除时间是 m+1 . 回收删掉的边的节点的话,空间就可以只开 n*2 了. #i ...
- LOJ#121. 「离线可过」动态图连通性(线段树分治)
题意 板子题,题意很清楚吧.. Sol 很显然可以直接上LCT.. 但是这题允许离线,于是就有了一个非常巧妙的离线的做法,好像叫什么线段树分治?? 此题中每条边出现的位置都可以看做是一段区间. 我们用 ...
- LOJ #121. 「离线可过」动态图连通性 LCT维护最大生成树
这个还是比较好理解的. 你考虑如果所有边构成一棵树的话直接用 LCT 模拟一波操作就行. 但是可能会出现环,于是我们就将插入/删除操作按照时间排序,然后依次进行. 那么,我们就要对我们维护的生成树改变 ...
- 【LOJ】#121. 「离线可过」动态图连通性
题解 和BZOJ4025挺像的 就是维护边权是时间的最大生成树 删边直接删 两点未联通时直接相连,两点联通则找两点间边权小的一条边删除即可 代码 #include <bits/stdc++.h& ...
- 【LOJ121】「离线可过」动态图连通性
[LOJ121]「离线可过」动态图连通性 题面 LOJ 题解 线段树分治的经典应用 可以发现每个边出现的时间是一个区间 而我们每个询问是一个点 所以我们将所有边的区间打到一颗线段树上面去 询问每个叶子 ...
- LOJ121 「离线可过」动态图连通性
思路 动态图连通性的板子,可惜我不会在线算法 离线可以使用线段树分治,每个边按照存在的时间插入线段树的对应节点中,最后再dfs一下求出解即可,注意并查集按秩合并可以支持撤销操作 由于大量使用STL跑的 ...
- 「LOJ 121」「离线可过」动态图连通性「按时间分治 」「并查集」
题意 你要维护一张\(n\)个点的无向简单图.你被要求执行\(m\)条操作,加入删除一条边及查询两个点是否连通. 0:加入一条边.保证它不存在. 1:删除一条边.保证它存在. 2:查询两个点是否联通. ...
- LOJ 546: 「LibreOJ β Round #7」网格图
题目传送门:LOJ #546. 题意简述: 题目说的很清楚了. 题解: 将不包含起点或障碍物的连续的行或列缩成一行或一列,不会影响答案. 处理过后,新的网格图的行数和列数最多为 \(2k + 3\). ...
- LOJ121 【离线可过】动态图连通性
题目链接:戳我 [线段树分治版本代码] 这里面的线段树是时间线段树,每一个节点都要开一个vector,记录当前时间区间中存在的边的标号qwq #include<iostream> #inc ...
随机推荐
- JBoss jmx-console中的秘密
JBoss jmx-console中的秘密 https://wenku.baidu.com/view/fe196f047cd184254b35351d.html
- Jquery Ajax 提交json数据
在MVC控制器(这里是TestController)下有一个CreateOrder的Action方法 [HttpPost] public ActionResult CreateOrder(List&l ...
- python文本文件处理和用户输入
#用户输入 a = input('please input: ') #这个输入什么即是什么,比如输入1,则a变量=1,输入'abc',则a变量 = 'abc',输入abc则报错,因为会把abc当做一个 ...
- TableView的cell加载倒计时重用问题解决方案
TableView的cell加载倒计时重用问题解决方案 效果 说明 1. 写过类似需求的朋友一定知道,TableView上面加载倒计时功能会遇到复杂的重用问题难以解决 2. 本人提供一种解决思路,高效 ...
- Python常见报错问题(不定时更新)
1.TabError: inconsistent use of tabs and spaces in indentation 在缩进中不一致地使用tab键和空格键. 报错原因:混用了tab和space ...
- November 16th 2016 Week 47th Wednesday
Success is falling nine times and getting up ten. 成功就是哪怕跌倒九次,也要在第十次爬起来. For most of us, we may choos ...
- 【笔记】select, poll, epool
Select 系统调用: select 轮询监听多个文件描述符的数组,其原理如下(转自:这里): 从用户空间拷贝fd_set到内核空间:注册回调函数__pollwait:遍历所有fd,对全部指定设备做 ...
- ASP.NET Core读取appsettings.json配置文件信息
1.在配置文件appsettings.json里新增AppSettings节点 { "Logging": { "LogLevel": { "Defau ...
- Jsp实现在线作业提交系统
Jsp实现在线作业提交系统 作为 Computer Science 的学生,凌晨四点之前睡都应该感到羞耻. 项目托管地址:https://github.com/four-in-the-morning/ ...
- module object has no attribute dumps的解决方法
问题产生原因: python的版本过低,其中的json包年久失修,需要更新 解决方法: 删除就的json包 >>> import json >>> print js ...