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 ...
随机推荐
- 将DataRow赋值给model中同名属性
/// <summary> /// 将DataRow赋值给model中同名属性 /// </summary> /// <typeparam name="T&qu ...
- [翻译] CNPPopupController
CNPPopupController CNPPopupController is a simple and versatile class for presenting a custom popup ...
- Django学习---抽屉热搜榜分析【all】
Python实例---抽屉热搜榜前端代码分析 Python实例---抽屉后台框架分析 Python学习---抽屉框架分析[点赞功能分析] Python学习---抽屉框架分析[数据库设计分析]18031 ...
- PHP安装posix、pctl扩展
安装问题 PHP Fatal error: Uncaught Error: Call to undefined function tsingsun\swoole\server\posix_kill() ...
- 【ORACLE】 安装需要注意的问题(一)
安装ORACLE虽然不是很难,但是有时候很容易因为一些小细节导致安装失败,浪费大量的时间. 这里总结一下安装ORACLE的时候需要注意的问题,以及解决的办法 问题1:系统先决条件检查 正在检查操作系统 ...
- VS2015 无法启动IIS Express Web服务器(已解决)
VS2015 无法启动IIS Express Web服务器 首先说一下我遇到问题的情况.这个项目是在公司电脑创建的,运行一直是正常的.今天把项目拷贝回来做. 可是到自己的电脑上,运行就提示 无法启动I ...
- 【笔记】JS数据类型总结
JavaScript有六种数据类型,分别为undefined.null.number.string.Boolean.object,前面的五种是基础数据类型,也称之为原始类型,也就是无法再细分的基本类型 ...
- mysql workbench的PK,NN,UQ,BIN,UN,ZF,AI
mysql workbench建表时PK.NN.UQ.BIN.UN.ZF.AI的意思,后面几个老搞不清,随记在这便于以后方便查. [intrinsic column flags] (基本字段类型标识) ...
- 【转】Android随笔之——PackageManager详解
参考:http://www.cnblogs.com/xingfuzzhd/p/3374504.html 今天要讲的是PackageManager.Android系统为我们提供了很多服务管理的类,包括A ...
- 【转】使用URL SCHEME启动天猫客户端并跳转到某个商品页面的方法
在项目中遇到了这样一个需求:让用户在手机应用中,点击一个天猫的商品链接(知道商品在PC浏览器里的地址),直接启动天猫的客户端并显示这个商品.以前曾经实现过类似的功能,不过那次是淘宝的商品,天猫和淘宝的 ...