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;
}
posted @ 2019-10-05 17:41  hankeke303  阅读(229)  评论(0编辑  收藏  举报