Lca + 树上差分
\(\color{Red}{本题的实质是看一条边属于几个环}\)
\(如图, 如何使标记的边的覆盖次数+c ?\)
\(我们把边权看成是其子树下所有点权之和,如果我需要增加x到p与y到p之间的边权,p=lca(x,y),\\那么只需要将x标记为x+c,y标记为y+c,p标记为p-2c\)
\(做完以上的操作,最后对整个树求一遍dfs即可,该过程类比于求前缀和\)
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define x first
#define y second
#define inf 0x3f3f3f3f
const int N = 100010, M = 2 * N;
int n, m;
int h[N], e[M], ne[M], idx;
int depth[N], fa[N][17];
int d[N];
int q[N];
int ans;
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void bfs() {
memset(depth, 0x3f, sizeof depth);
depth[0] = 0; depth[1] = 1;
int hh = 0, tt = 0;
q[0] = 1;
while (hh <= tt) {
int t = q[hh++];
for (int i = h[t]; ~i; i = ne[i]) {
int j = e[i];
if (depth[j] > depth[t] + 1) {
depth[j] = depth[t] + 1;
q[++tt] = j;
fa[j][0] = t;
for (int k = 1; k <= 16; ++k)
fa[j][k] = fa[fa[j][k - 1]][k - 1];
}
}
}
}
int lca(int a, int b) {
if (depth[a] < depth[b]) swap(a, b);
for (int k = 16; k >= 0; --k)
if (depth[fa[a][k]] >= depth[b])
a = fa[a][k];
if (a == b) return a;
for (int k = 16; k >= 0; --k)
if (fa[a][k] != fa[b][k]) {
a = fa[a][k];
b = fa[b][k];
}
return fa[a][0];
}
int dfs(int u, int father) {
int res = d[u];
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (j != father) {
int s = dfs(j, u);
if (s == 0) ans += m;
else if (s == 1) ans++;
res += s;
}
}
return res;
}
int main() {
IO;
cin >> n >> m;
memset(h, -1, sizeof h);
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
add(a, b), add(b, a);
}
bfs();
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
int p = lca(a, b);
d[a]++, d[b]++, d[p] -= 2;
}
dfs(1, -1);
cout << ans << '\n';
return 0;
}