bzoj1969 [Ahoi2005]LANE 航线规划 树链剖分
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=1969
题解
如果我们把整个图边双联通地缩点,那么最终会形成一棵树的样子。
那么在这棵树上,\(x\) 和 \(y\) 两个点的答案就是它们之间的不在环中的边的数量。
现在考虑动态维护每一条边在不在环中。发现动态删除的话不太好做,所以时光反转,改成插入一条边。
先随便建立一棵生成树,然后如果插入一条非树边,那么两个端点之间的边就都是在环中的边了。
用树剖维护就可以了。
时间复杂度 \(O(q\log^2 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;
}
#define lc o << 1
#define rc o << 1 | 1
const int N = 30000 + 7;
const int M = 100000 + 7;
const int QQ = 40000 + 7;
int n, m, Q, dfc;
int dep[N], f[N], siz[N], son[N], dfn[N], top[N], pre[N];
int ans[QQ];
std::set<pii> ss;
struct Query {int opt, x, y; } qq[QQ];
struct Edge { int to, ne; } g[M << 1]; int head[N], tot;
inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y) { addedge(x, y), addedge(y, x); }
inline void dfs1(int x, int fa = 0) {
dep[x] = dep[fa] + 1, f[x] = fa, siz[x] = 1;
for fec(i, x, y) if (y != fa && !dep[y] && !ss.count(pii(x, y))) dfs1(y, x), siz[x] += siz[y], siz[y] > siz[son[x]] && (son[x] = y);
}
inline void dfs2(int x, int pa) {
dfn[x] = ++dfc, top[x] = pa, pre[dfc] = x;
if (!son[x]) return; dfs2(son[x], pa);
for fec(i, x, y) if (y != f[x] && y != son[x] && !dfn[y] && !ss.count(pii(x, y))) dfs2(y, y);
}
struct Node { int sum, set; } t[N << 2];
inline void build(int o, int L, int R) {
t[o].set = 0;
if (L == R) return t[o].sum = 1, (void)0;
int M = (L + R) >> 1;
build(lc, L, M), build(rc, M + 1, R);
t[o].sum = t[lc].sum + t[rc].sum;
}
inline void qset(int o, int L, int R, int l, int r) {
if (l > r) return;
if (t[o].set) return;
if (l <= L && R <= r) return t[o].sum = 0, t[o].set = 1, (void)0;
int M = (L + R) >> 1;
if (l <= M) qset(lc, L, M, l, r);
if (r > M) qset(rc, M + 1, R, l, r);
t[o].sum = t[lc].sum + t[rc].sum;
}
inline int qsum(int o, int L, int R, int l, int r) {
if (l > r) return 0;
if (t[o].set) return 0;
if (l <= L && R <= r) return t[o].sum;
int M = (L + R) >> 1;
if (r <= M) return qsum(lc, L, M, l, r);
if (l > M) return qsum(rc, M + 1, R, l, r);
return qsum(lc, L, M, l, r) + qsum(rc, M + 1, R, l, r);
}
inline void upd(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) std::swap(x, y);
qset(1, 1, n, dfn[top[x]], dfn[x]);
x = f[top[x]];
}
if (dep[x] > dep[y]) std::swap(x, y);
qset(1, 1, n, dfn[son[x]], dfn[y]);
}
inline int qry(int x, int y) {
int ans = 0;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) std::swap(x, y);
ans += qsum(1, 1, n, dfn[top[x]], dfn[x]);
x = f[top[x]];
}
if (dep[x] > dep[y]) std::swap(x, y);
return ans += x != y ? qsum(1, 1, n, dfn[son[x]], dfn[y]) : 0;
}
inline void work() {
dfs1(1), dfs2(1, 1), build(1, 1, n);
for (int x = 1; x <= n; ++x) for fec(i, x, y)
if (x < y && f[x] != y && f[y] != x && !ss.count(pii(x, y))) upd(x, y);
while (Q) {
int opt = qq[Q].opt, x = qq[Q].x, y = qq[Q].y;
--Q;
if (opt == 0) upd(x, y);
else ans[++ans[0]] = qry(x, y);
}
while (ans[0]) printf("%d\n", ans[ans[0]--]);
}
inline void init() {
read(n), read(m);
int opt, x, y;
for (int i = 1; i <= m; ++i) read(x), read(y), adde(x, y);
while (read(opt), ~opt) {
read(x), read(y);
if (opt == 0) ss.insert(pii(x, y)), ss.insert(pii(y, x));
qq[++Q] = (Query){ opt, x, y };
}
}
int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}
bzoj1969 [Ahoi2005]LANE 航线规划 树链剖分的更多相关文章
- BZOJ 1969: [Ahoi2005]LANE 航线规划( 树链剖分 )
首先我们要时光倒流, 倒着做, 变成加边操作维护关键边. 先随意搞出一颗树, 树上每条边都是关键边(因为是树, 去掉就不连通了)....然后加边(u, v)时, 路径(u, v)上的所有边都变成非关键 ...
- 【bzoj1959】[Ahoi2005]LANE 航线规划 树链剖分+线段树
题目描述 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系——一个巨大的由千百万星球构成的Samuel星系. 星际空间站的Samuel II巨型计算 ...
- BZOJ 1969: [Ahoi2005]LANE 航线规划 [树链剖分 时间倒流]
题意: 一张图,删除边,求两点之间的割边数量.保证任意时刻图连通 任求一棵生成树,只有树边可能是割边 时间倒流,加入一条边,就是两点路径上的边都不可能是割边,区间覆盖... 然后本题需要把边哈希一下, ...
- bzoj1969: [Ahoi2005]LANE 航线规划(树链剖分)
只有删边,想到时间倒流. 倒着加边,因为保证图连通,所以一开始一定至少是一棵树.我们先建一棵树出来,对于每一条非树边,两个端点在树上这段路径的边就不会变成关键边了,所以将它们对答案的贡献删去,那么直接 ...
- [bzoj1969] [Ahoi2005]LANE 航线规划
tarjan.并查集.树状数组.树链剖分. 时间倒流,变删边为加边. 先求一波边双联通分量,缩点. 题目保证最后还是整张图联通的..所以就是一棵树. 现在的操作就是,将路径上的边权置0(加边时),查询 ...
- 洛谷 P2542 [AHOI2005]航线规划 树链剖分_线段树_时光倒流_离线
Code: #include <map> #include <cstdio> #include <algorithm> #include <cstring&g ...
- BZOJ1969: [Ahoi2005]LANE 航线规划(LCT)
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 587 Solved: 259[Submit][Status][Discuss] Description ...
- luogu2542 航线规划 (树链剖分)
不会lct,所以只能树剖乱搞 一般这种删边的题都是离线倒着做,变成加边 他要求的结果其实就是缩点以后两点间的距离. 然后先根据最后剩下的边随便做出一个生成树,然后假装把剩下的边当成加边操作以后处理 这 ...
- 【BZOJ1969】[Ahoi2005]LANE 航线规划 离线+树链剖分+线段树
[BZOJ1969][Ahoi2005]LANE 航线规划 Description 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系——一个巨大的由 ...
随机推荐
- 力扣60——第k个排列
原题 给出集合 [1,2,3,-,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: 1. "123" 2. &qu ...
- CSS中clip-path属性的使用
clip-path的使用 polygon 值为多个坐标点组成,坐标第一个值是x方向,第二个值是y方向. 左上角为原点,右下角是(100%,100%)的点.</p> body { backg ...
- SqlServer2012 File Table文件表
SQL Server 2012 提供一种特殊的“文件表”,也称为“FileTable”. FileTable 是一种专用的用户表,它包含存储 FILESTREAM 数据的预定义架构以及文件和目录层次结 ...
- React-Native 之 GD (十七)小时风云榜按钮处理
小时风云榜按钮处理 在服务器返回给我们的 json 数据中,提供了 hasnexthour 字段,当这个字段返回为 1 的时候,表示后面还有内容,按钮可以点击,否则不能点击,按照这个思路,我们就来完成 ...
- 【tensorflow使用笔记二】:tensorflow中input_data.py代码有问题的解决方法
由于input_data网页打不开,因此从博客找到代码copy: https://blog.csdn.net/weixin_43159628/article/details/83241345 将代码放 ...
- 运行jar包shell脚本
#!/bin/sh #该文件必须放在jar包的目录下,因为是以相对路径来运行的.不放jar包目录的话,可以直接在jar_name参数写绝对路径 #start 设置三个参数 #环境 profile=te ...
- centos7安装kafka
1.官网或 wget 下载 kafka_2.12-2.2.0.tgz 二进制代码包 cd /home/tar wget http://mirror.bit.edu.cn/apache/kafka/2. ...
- mysql依据某一张表的字段,查询出对应的表所在的数据库
表太多,只记得这个表有一个mygame的字段,但是并不知道这张表在那个数据库下,只能根据这个字段查找对应的表和所在数据库 select table_schema,table_name from inf ...
- 启动tomcat报错One or more listeners failed to start,却找不到任何错误日志的临时解决方案
在整合spring和quartz时,启动tomcat,服务台报以上错误,却找不到任何错误日志…… 参考了https://www.cnblogs.com/sxdcgaq8080/p/8005886.ht ...
- list、tuple、dict加*星号
*相当于解压的作用,当list中只有一个元素时,利用*作用相当于直接去[],去[]后的数据类型是字符串. _list = [1,2,3] _tuple = (1,2,3) _dict = {1:'a' ...