题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=4998

题解

根据题意,就是要动态维护点双,求出一个点双的权值和。

所以这道题就是和 bzoj2959 长跑 一样了,每一次枚举一条路径上的点双暴力和合并,并查集维护。

本来是想再练一练 LCT 码力的,结果又调了半天。

大概错误都有:

  1. connect 函数的 \(fa\) 和 \(o\) 的顺序写错;
  2. 每一次把一条链合并成一个点双的时候需要把代表点的孩子删掉!!!
  3. 要把连边和 dfs 一起写,因为 dfs 的时候初始点双还没有分清楚。

时间复杂度 \(O(m\log 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;
} const int N = 200000 + 7; #define lc c[0]
#define rc c[1] int n, m, Q, tp, dfc, bccno;
int a[N], st[N], stt[N], dfn[N], low[N], bcc[N];
int fa[N]; struct Edge { int to, ne; } g[N << 1]; int head[N], tot = 1;
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 int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } struct Node { int c[2], fa, rev, v, s, sum; } t[N];
inline bool isroot(int o) { return t[find(t[o].fa)].lc != o && t[find(t[o].fa)].rc != o; }
inline bool idtfy(int o) { return t[find(t[o].fa)].rc == o; }
inline void connect(int fa, int o, int d) { t[fa].c[d] = o, t[o].fa = fa; }
inline void pushup(int o) {
assert(find(o) == o);
t[o].s = t[t[o].lc].s + t[t[o].rc].s + 1;
t[o].sum = t[t[o].lc].sum + t[t[o].rc].sum + t[o].v;
}
inline void pushdown(int o) {
if (!t[o].rev) return;
if (t[o].lc) t[t[o].lc].rev ^= 1, std::swap(t[t[o].lc].lc, t[t[o].lc].rc);
if (t[o].rc) t[t[o].rc].rev ^= 1, std::swap(t[t[o].rc].lc, t[t[o].rc].rc);
t[o].rev = 0;
}
inline void rotate(int o) {
assert(find(o) == o);
assert(!isroot(o));
int fa = find(t[o].fa), pa = find(t[fa].fa), d1 = idtfy(o), d2 = idtfy(fa), b = t[o].c[d1 ^ 1];
// dbg("o = %d, fa = %d, pa = %d, d1 = %d, d2 = %d, b = %d\n", o, fa, pa, d1, d2, b);
if (!isroot(fa)) t[pa].c[d2] = o; t[o].fa = pa;
connect(o, fa, d1 ^ 1), connect(fa, b, d1);
pushup(fa), pushup(o);
assert(!t[0].lc && !t[0].rc);
assert(t[o].fa != o), assert(t[b].fa != b), assert(t[fa].fa != fa), assert(t[pa].fa != pa);
}
inline void splay(int o) {
// dbg("splay: o = %d, t[o].fa = %d\n", o, t[o].fa);
int x = o, tp = 0;
stt[++tp] = x;
while (!isroot(x)) /*dbg("splay_stakc: o = %d, x = %d, t[x].fa = %d, find(t[x].fa) = %d\n", o, x, t[x].fa, find(t[x].fa)), */stt[++tp] = x = find(t[x].fa);
while (tp) pushdown(stt[tp--]);
while (!isroot(o)) {
int fa = find(t[o].fa);
// dbg(": o = %d, fa = %d, isroot(o) = %d, isroot(fa) = %d\n", o, fa, (int)isroot(o), (int)isroot(fa));
if (isroot(fa)) rotate(o);
else if (idtfy(o) == idtfy(fa)) rotate(fa), rotate(o);
else rotate(o), rotate(o);
}
}
inline void access(int o) {
// dbg("**** begin access, o = %d\n", o);
for (int x = 0; o; o = find(t[x = o].fa))
splay(o), t[o].rc = x, pushup(o);
// dbg("**** end access\n");
}
inline void mkrt(int o) {
access(o), splay(o);
t[o].rev ^= 1, std::swap(t[o].lc, t[o].rc);
}
inline int getrt(int o) {
access(o), splay(o);
while (pushdown(o), t[o].lc) o = t[o].lc;
return splay(o), o;
}
inline void link(int x, int y) {
// dbg("x = %d, y = %d\n", x, y);
mkrt(x);
if (getrt(y) != x) t[x].fa = y;
} inline void dfs1(int x, int fr = 0) {
// dbg("x = %d, fr = %d\n", x, fr);
dfn[x] = low[x] = ++dfc, st[++tp] = x;
for fec(i, x, y) if ((i ^ fr) != 1) {
if (!dfn[y]) dfs1(y, i), smin(low[x], low[y]);
else if (!bcc[y]) smin(low[x], dfn[y]);
}
if (dfn[x] == low[x]) {
++bccno;
while (1) {
int y = st[tp--];
bcc[y] = bccno, fa[y] = x, x != y && (t[x].v += t[y].v);
// dbg("is bcc : x = %d, y = %d, tp = %d, st* = %d\n", x, y, tp, st[1]);;
if (x == y) break;
}
pushup(x);
}
} inline void dfs2(int o) {
if (!o) return;
assert(find(o) == o);
pushdown(o);
dfs2(t[o].lc);
st[++tp] = o;
dfs2(t[o].rc);
} inline void work() {
for (int i = 1; i <= n; ++i) if (!dfn[i]) dfs1(i);
for (int x = 1; x <= n; ++x) for fec(i, x, y) link(find(x), find(y));
// dbg("*****\n");
while (Q--) {
int x, y;
read(x), read(y);
// dbg("x = %d, y = %d\n", x, y);
x = find(x), y = find(y);
if (getrt(x) != getrt(y)) link(x, y), puts("No");
else if (x == y) printf("%d\n", t[x].v);
else {
// dbg("query : x = %d, y = %d\n", x, y);
mkrt(x), access(y), splay(y);
tp = 0, dfs2(y);
for (int i = 1; i < tp; ++i) t[st[tp]].v += t[st[i]].v, fa[st[i]] = st[tp];
t[st[tp]].lc = 0, pushup(st[tp]), printf("%d\n", t[st[tp]].v);
// dbg("************* x = %d, y = %d, st[1] = %d, %d\n", x, y, st[tp], t[st[tp]].fa);
}
}
} inline void init() {
read(n), read(m), read(Q);
int x, y;
for (int i = 1; i <= m; ++i) read(x), read(y), adde(x, y);
for (int i = 1; i <= n; ++i) fa[i] = i, t[i].v = 1, pushup(i);
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

最新文章

  1. IOS的MVC和MVVM模式简明介绍
  2. textarea自适应高度
  3. 之三:CAAnimationGroup - 动画组
  4. .net开发中常用的第三方组件
  5. Shell脚本_启动停止重启sh脚本
  6. golang heap container balance request
  7. C# 格式化字符串,日期,字符串操作汇总
  8. codeforce626D (概率)
  9. Mongo报如下类似错误时的修改方法Cannot natively represent the long 1396367483000 on this platform
  10. Java主流日志工具库
  11. Leetcode: Remove Elements
  12. [转载]Android 异步加载解决方案
  13. oracle数据库连接
  14. VxWorks6.6 pcPentium BSP 使用说明(二):创建启动盘
  15. PID控制学习笔记(一)
  16. Javascript事件模型(三):JavaScript事件绑定方法总结(及Jquery)
  17. Webstorm 提示 Can&#39;t use Subversion command line client
  18. Cordova 项目 加载不出XML文件
  19. jq 绑定事件和解绑事件
  20. 标志寄存器在Debug中的表示

热门文章

  1. Go开发[八]goroutine和channel
  2. Java定时器Timer
  3. oracle dis系列课程总结
  4. Shiro 学习
  5. css之——div模拟textarea文本域的实现
  6. FLUME安装&amp;环境(二):拉取MySQL数据库数据到Kafka
  7. Chapter02 第一节 开始学习C++
  8. [转帖]CentOS 7 使用kubeadm 部署 Kubernetes
  9. openstack镜像服务(glance)
  10. Sql server 执行计划详解