「一本通 4.4 例 2」暗的连锁 POJ3417 LOJ10131
树上边的差分,给每个点赋值为0,对于非树边(x,y)x,y权值+1,lca(x,y)-=2,再dfs
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define MN 200050
using namespace std;
int n, m, cnt, head[MN], ans[MN], dfn[MN], num[MN];
int f[MN][21];
int lg[MN];
struct tu {
int v, nxt;
} e[MN];
void add(int u, int v) {
e[++cnt].v = v;
e[cnt].nxt = head[u];
head[u] = cnt;
}
void dfs(int now, int fa) {
dfn[now] = dfn[fa] + 1;
f[now][0] = fa;
for (int i = 1; (1 << i) <= dfn[now]; i++) f[now][i] = f[f[now][i - 1]][i - 1];
for (int i = head[now]; i; i = e[i].nxt) {
if (e[i].v != fa)
dfs(e[i].v, now);
}
}
int lca(int x, int y) {
if (dfn[x] < dfn[y])
swap(x,