题目传送门

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;
}

bzoj4998 星球联盟 LCT + 并查集的更多相关文章

  1. BZOJ4998星球联盟——LCT+并查集(LCT动态维护边双连通分量)

    题目描述 在遥远的S星系中一共有N个星球,编号为1…N.其中的一些星球决定组成联盟,以方便相互间的交流.但是,组成 联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太空隧道连接两 ...

  2. 【bzoj4998】星球联盟 LCT+并查集

    题目描述 在遥远的S星系中一共有N个星球,编号为1…N.其中的一些星球决定组成联盟,以方便相互间的交流.但是,组成联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太空隧道连接两个 ...

  3. 【bzoj4998】星球联盟(并查集+边双)

    题面 传送门 题解 总算有自己的\(bzoj\)账号啦! 话说这题好像\(Scape\)去年暑假就讲过--然而我到现在才会-- \(LCT\)什么的跑得太慢了而且我也不会,所以这里是一个并查集的做法 ...

  4. bzoj4998 星球联盟

    bzoj4998 星球联盟 原题链接 题解 先按照输入顺序建一棵树(森林),然后用一个并查集维护联盟的关系,对于不是树上的边\(a-b\),就把\(a-lca(a,b),b-lca(a,b)\)全部合 ...

  5. 【bzoj2959】长跑 LCT+并查集

    题目描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘,摩肩接踵,盛况空前.为了 ...

  6. BZOJ4998 星球联盟(LCT+双连通分量+并查集)

    即要求动态维护边双.出现环时将路径上的点合并即可.LCT维护.具体地,加边成环时makeroot+access+splay一套把这段路径提出来,暴力dfs修改并查集祖先,并将这部分与根断开,视为删除这 ...

  7. 【BZOJ2049】 [Sdoi2008]Cave 洞穴勘测 LCT/并查集

    两种方法: 1.LCT 第一次LCT,只有link-cut和询问,无限T,到COGS上找了数据,发现splay里的父亲特判出错了(MD纸张),A了,好奇的删了反转T了.... #include < ...

  8. 【BZOJ】2049: [Sdoi2008]Cave 洞穴勘测(lct/并查集)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2049 bzoj挂了..在wikioi提交,,1A-写lct的速度越来越快了-都不用debug-- 新 ...

  9. BZOJ_2049_[Sdoi_2008]_Cave_洞穴勘测_(LCT/并查集)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=2049 给出一个森林,起始互不相连,现在有link和cut两种操作,问x,y是否在一棵树里. 分 ...

随机推荐

  1. windows的 附件到底是什么东东?

    附件, 包括其父目录"所有程序" -> "开始菜单", 其实都是一个目录而已!! 要对"开始菜单"下的所有内容进行 自定义 : 添加删 ...

  2. leetcode 206 反转链表 Reverse Linked List

    C++解法一:迭代法,使用前驱指针pre,当前指针cur,临时后继指针nxt: /** * Definition for singly-linked list. * struct ListNode { ...

  3. Python Module_pdb_DEBUG 方法

    目录 目录 pdb pdb 的 Debug 方式 pdb 的调试指令 示例 IPython 自带的 Debug 工具 ipdb pdb pdb 是 Python 自带的程序包,为 Python 程序提 ...

  4. IDEA无法导入HttpServlet包解决方法

    1.maven项目 直接在pom.xml中添加对java servlet api的依赖即可,比较常用的一个servlet版本3.1.0的依赖如下: <!-- https://mvnreposit ...

  5. xmake入门,构建项目原来可以如此简单

    前言 在开发xmake之前,我一直在使用gnumake/makefile来维护个人C/C++项目,一开始还好,然而等项目越来越庞大后,维护起来就非常吃力了,后续也用过一阵子automake系列工具,并 ...

  6. 第九周总结&第七次实验报告

    实验7 实验任务详情: 完成火车站售票程序的模拟. 要求: (1)总票数1000张: (2)10个窗口同时开始卖票: (3)卖票过程延时1秒钟: (4)不能出现一票多卖或卖出负数号票的情况. 实验过程 ...

  7. Linux /dev/shm

    /dev/shm是Linux下的一个目录,/dev/shm目录不在磁盘上,而是在内存中,因此使用Linux /dev/shm的效率非常高,直接写进内存 可以通过两个脚本验证Linux /dev/shm ...

  8. java8-----lambda语法

    // -----lambda语法1------ https://www.baidu.com/link?url=6iszXQlsmyaoWVZMaPs3g8vLRQXzdzTnKzQYTF8lg-5QQ ...

  9. 51nod1769 Clarke and math 2

    题目 实际上就是要求\(f*I^k\). 因为\(I^k\)是一个积性函数,所以我们只需要考虑如何求\(I^k(p^a)\). 把这个东西转化成一个长度为\(k\)的序列,每一位是\(\frac{i_ ...

  10. yaf框架安装

    第一步:明白yaf框架是以扩展的形式要先配置到php里面,对于windows系统的使用者,首先要去官网:http://code.google.com/p/yafphp/downloads/list如果 ...