题面

话说#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.「离线可过」动态图连通性的更多相关文章

  1. LOJ 121 「离线可过」动态图连通性——LCT维护删除时间最大生成树 / 线段树分治

    题目:https://loj.ac/problem/121 离线,LCT维护删除时间最大生成树即可.注意没有被删的边的删除时间是 m+1 . 回收删掉的边的节点的话,空间就可以只开 n*2 了. #i ...

  2. LOJ#121. 「离线可过」动态图连通性(线段树分治)

    题意 板子题,题意很清楚吧.. Sol 很显然可以直接上LCT.. 但是这题允许离线,于是就有了一个非常巧妙的离线的做法,好像叫什么线段树分治?? 此题中每条边出现的位置都可以看做是一段区间. 我们用 ...

  3. LOJ #121. 「离线可过」动态图连通性 LCT维护最大生成树

    这个还是比较好理解的. 你考虑如果所有边构成一棵树的话直接用 LCT 模拟一波操作就行. 但是可能会出现环,于是我们就将插入/删除操作按照时间排序,然后依次进行. 那么,我们就要对我们维护的生成树改变 ...

  4. 【LOJ】#121. 「离线可过」动态图连通性

    题解 和BZOJ4025挺像的 就是维护边权是时间的最大生成树 删边直接删 两点未联通时直接相连,两点联通则找两点间边权小的一条边删除即可 代码 #include <bits/stdc++.h& ...

  5. 【LOJ121】「离线可过」动态图连通性

    [LOJ121]「离线可过」动态图连通性 题面 LOJ 题解 线段树分治的经典应用 可以发现每个边出现的时间是一个区间 而我们每个询问是一个点 所以我们将所有边的区间打到一颗线段树上面去 询问每个叶子 ...

  6. LOJ121 「离线可过」动态图连通性

    思路 动态图连通性的板子,可惜我不会在线算法 离线可以使用线段树分治,每个边按照存在的时间插入线段树的对应节点中,最后再dfs一下求出解即可,注意并查集按秩合并可以支持撤销操作 由于大量使用STL跑的 ...

  7. 「LOJ 121」「离线可过」动态图连通性「按时间分治 」「并查集」

    题意 你要维护一张\(n\)个点的无向简单图.你被要求执行\(m\)条操作,加入删除一条边及查询两个点是否连通. 0:加入一条边.保证它不存在. 1:删除一条边.保证它存在. 2:查询两个点是否联通. ...

  8. LOJ 546: 「LibreOJ β Round #7」网格图

    题目传送门:LOJ #546. 题意简述: 题目说的很清楚了. 题解: 将不包含起点或障碍物的连续的行或列缩成一行或一列,不会影响答案. 处理过后,新的网格图的行数和列数最多为 \(2k + 3\). ...

  9. LOJ121 【离线可过】动态图连通性

    题目链接:戳我 [线段树分治版本代码] 这里面的线段树是时间线段树,每一个节点都要开一个vector,记录当前时间区间中存在的边的标号qwq #include<iostream> #inc ...

随机推荐

  1. python 下字符串格式时间比较

    python 下有多个有关时间的模块,分别是time.datetime.calendar,今天重点讨论下time写法. 其中time模块,主要有以下方法: ltime=time.time() 获取当前 ...

  2. Mysql源码安装shell脚本

    #!/bin/bash#date:2019/1/20#by author zhangjia#install mysql#shell_name:mysql_auto_install.sh######## ...

  3. PHP解决网站大数据大流量与高并发

    1:硬件方面 普通的一个p4的服务器每天最多能支持10万左右的IP,如果访问量超过10W那么需要专用的服务器才能解决,如果硬件不给力软件怎么优化都是于事无补的.主要影响服务器的速度 有:网络-硬盘读写 ...

  4. 我的第一个springboot应用+maven依赖管理

    第一步:使用Eclipse创建maven工程SpringBootFirst:工程目录如下 第二步:编写依赖文件pom.xml <project xmlns="http://maven. ...

  5. svn检出项目,Project *** is already imported into workspace

    1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.如果从svn检出的项目   Import  ----  Existing Maven Pro ...

  6. September 18th 2017 Week 38th Monday

    The only person you should try to be better than is the person you were yesterday. 你唯一应该试着去超越的人,是昨天的 ...

  7. python3编程的一些实用技巧1

    1.choice函数:返回一个列表,元组,字符串的随机项   :   调用时应导入random模块,如from random import choice 2.print 两个字符串, 逗号,+号进行连 ...

  8. [转]Hadoop 读写数据流

    Hadoop文件读取 1)客户端通过调用FileSystem对象中的open()函数来读取它做需要的数据.FileSystem是HDFS中DistributedFileSystem的一个实例. 2)D ...

  9. tomcat 开启远程debug

    修改 tomcat  目录下  /bin/catelina.sh #                   execution immediately after startup. Default is ...

  10. python第十六课——ascii码

    2.ascii码 美国设计出来的一张编码表,将涉及的字符都编号了,底层仍然还是进行二进制的运算: 记住:3个范围段 1).'0' --> 码值:48 2).'A' --> 码值:65 3) ...