bzoj3772 精神污染 dfs 序+主席树
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=3772
题解
很简单的一道题目。
上午研究一个题目的时候发现了这个题目是一个弱化版,所以来写了一下。
一开始以为能半个小时写完调玩,结果从上午 11:00 开始写,写到现在。
考虑求出每一个条路径会被几条路径包含。
显然满足条件的路径两个端点必须分别在原路径的两个端点各自的子树中(这个子树指的是以另一个点为根的时候的子树)。
这个子树的 dfs 序有一定的连续性,用主席树维护就可以了。
时间复杂度 \(O(n^2)\)。
#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 = 100000 + 7;
int n, m, dfc, nod;
int dep[N], f[N], siz[N], son[N], dfn[N], pre[N], top[N];
int rt[N];
std::vector<int> v[N];
struct Edge { int to, ne; } g[N << 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); }
struct Node { int lc, rc, val; } t[N * 40];
inline void ins(int &o, int L, int R, int x, int k) {
t[++nod] = t[o], t[o = nod].val += k;
if (L == R) return;
int M = (L + R) >> 1;
if (x <= M) ins(t[o].lc, L, M, x, k);
else ins(t[o].rc, M + 1, R, x, k);
}
inline int qsum(int o, int p, int L, int R, int l, int r) {
// dbg("o = %d, p = %d, L = %d, R = %d, l = %d, r = %d, t[o].val = %d, t[p].val = %d\n", o, p, L, R, l, r, t[o].val, t[p].val);
assert(t[o].val >= t[p].val);
if (l > r) return 0;
if (l <= L && R <= r) return t[o].val - t[p].val;
int M = (L + R) >> 1;
if (r <= M) return qsum(t[o].lc, t[p].lc, L, M, l, r);
if (l > M) return qsum(t[o].rc, t[p].rc, M + 1, R, l, r);
return qsum(t[o].lc, t[p].lc, L, M, l, r) + qsum(t[o].rc, t[p].rc, M + 1, R, l, r);
}
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) dfs1(y, x), siz[x] += siz[y], siz[y] > siz[son[x]] && (son[x] = y);
}
inline void dfs2(int x, int pa) {
top[x] = pa, dfn[x] = ++dfc, pre[dfc] = x;
if (!son[x]) return; dfs2(son[x], pa);
for fec(i, x, y) if (y != f[x] && y != son[x]) dfs2(y, y);
}
inline int lca(int x, int y) {
while (top[x] != top[y]) dep[top[x]] > dep[top[y]] ? x = f[top[x]] : y = f[top[y]];
return dep[x] < dep[y] ? x : y;
}
inline int gson(int x, int p) {
int g = 0;
while (top[x] != top[p]) g = top[x], x = f[top[x]];
return x == p ? g : son[p];
}
inline void work() {
dfs1(1), dfs2(1, 1);
for (int i = 1; i <= n; ++i) {
rt[i] = rt[i - 1];
int x = pre[i];
for (std::vector<int>::iterator p = v[x].begin(); p != v[x].end(); ++p)
ins(rt[i], 1, n, dfn[*p], 1);// , dbg("i = %d, x = %d, *p = %d\n", i, x, *p);
}
ll ans = 0;
for (int x = 1; x <= n; ++x) {
for (std::vector<int>::iterator it = v[x].begin(); it != v[x].end(); ++it) {
int y = *it, p = lca(x, y);
if (dep[x] > dep[y] || (dep[x] == dep[y] && x > y)) continue;
// dbg("x = %d, y = %d, p = %d\n", x, y, p);
if (x != p) ans += qsum(rt[dfn[y] + siz[y] - 1], rt[dfn[y] - 1], 1, n, dfn[x], dfn[x] + siz[x] - 1);
else {
p = gson(y, x);
ans += qsum(rt[dfn[y] + siz[y] - 1], rt[dfn[y] - 1], 1, n, 1, dfn[p] - 1);
ans += qsum(rt[dfn[y] + siz[y] - 1], rt[dfn[y] - 1], 1, n, dfn[p] + siz[p], n);
}
--ans;
// dbg("ans = %lld\n", ans);
}
}
ll cnt = (ll)m * (m - 1) / 2, p = std::__gcd(ans, cnt);
printf("%lld/%lld\n", ans / p, cnt / p);
// dbg("***************** %lld, %lld\n", ans, cnt);
}
inline void init() {
read(n), read(m);
int x, y;
for (int i = 1; i < n; ++i) read(x), read(y), adde(x, y);
for (int i = 1; i <= m; ++i) {
read(x), read(y);
v[x].pb(y);
if (x != y) v[y].pb(x);
}
}
int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}