有一棵以 1 为根节点,具有 n 个节点的有根树,树上的每条边都具有边权,初始时所有边权均为 0。共有 m 次操作,每次操作将选定两个不同的节点 u,v,使从 u 到 v 的简单路径上的所有边权值均 +1。求每次操作结束后,每条边的权值。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int top[N], son[N], fa[N], siz[N], dep[N], dfn[N], rnk[N], dfncnt, tmp[N], w[N];
int head[N], cnt, n, m;
struct node {
int v, ne;
}e[N<<1];
void add(int u, int v) {
e[++cnt].v = v, e[cnt].ne = head[u], head[u] = cnt;
}
struct e_node {
int u, v;
}id[N];
struct segtree {
int sum[N << 2], add[N << 2];
void build(int root, int left, int right) {
if (left == right) {
sum[root] = 0; add[root] = 0; return;
}
int mid = left + right >> 1;
build(root << 1, left, mid);
build(root << 1 | 1, mid + 1, right);
sum[root] = sum[root << 1] + sum[root << 1 | 1];
}
void pushdown(int root, int left, int right) {
if (!add[root]) return;
int mid = left + right >> 1;
sum[root << 1] += (mid - left + 1) * add[root];
add[root << 1] += add[root];
sum[root << 1 | 1] += (right - mid) * add[root];
add[root << 1 | 1] += add[root];
add[root] = 0;
}
void update(int root, int left, int right, int l, int r, int val) {
if (l <= left && right <= r) {
sum[root] += (right - left + 1) * val;
add[root] += val; return;
}
pushdown(root, left, right);
int mid = left + right >> 1;
if (l <= mid) update(root << 1, left, mid, l, r, val);
if (r > mid) update(root << 1 | 1, mid + 1, right, l, r, val);
sum[root] = sum[root << 1] + sum[root << 1 | 1];
}
int query(int root, int left, int right, int id) {
if (left == right && id == left) {
return sum[root];
}
pushdown(root, left, right);
int mid = left + right >> 1;
if (id <= mid) return query(root<<1, left, mid, id);
else return query(root<<1|1, mid + 1, right, id);
}
}tr;
void dfs1(int now) {
siz[now] = 1;
for (int i = head[now]; i; i = e[i].ne) {
if (dep[e[i].v]) continue;
dep[e[i].v] = dep[now] + 1;
fa[e[i].v] = now;
dfs1(e[i].v);
tmp[e[i].v] = 0; //用子节点映射边权值
siz[now] += siz[e[i].v];
if (!son[now] || siz[son[now]] < siz[e[i].v]) son[now] = e[i].v;
}
}
void dfs2(int now, int tp) {
top[now] = tp;
dfn[now] = ++dfncnt;
w[dfncnt] = tmp[now]; //映射当前dfn对应的原标号的对应边的值
rnk[dfncnt] = now; //映射当前dfn对应的原标号
if (son[now]) dfs2(son[now], tp);
for (int i = head[now]; i; i = e[i].ne) {
if (e[i].v == son[now] || e[i].v == fa[now]) continue;
dfs2(e[i].v, e[i].v);
}
}
void update(int x, int y) {
int fx = top[x], fy = top[y];
while (fx != fy) {
if (dep[fx] >= dep[fy]) tr.update(1, 1, n, dfn[fx], dfn[x], 1), x = fa[fx];
else tr.update(1, 1, n, dfn[fy], dfn[y], 1), y = fa[fy];
fx = top[x], fy = top[y];
}
if (dep[x] > dep[y]) swap(x, y);
if (x != y) tr.update(1, 1, n, dfn[x] + 1, dfn[y], 1);
}
int main() {
cin >> n;
for (int i = 1; i <= n - 1; i++) {
int u, v; cin >> u >> v;
add(u, v), add(v, u);
id[i].u = u, id[i].v = v;
}
dep[1] = 1, dfs1(1), dfs2(1, 1);
tr.build(1, 1, n);
cin >> m;
while (m--) {
int u, v; cin >> u >> v;
update(u, v);
for (int i = 1; i <= n - 1; i++) {
int po = dep[id[i].u] > dep[id[i].v] ? id[i].u : id[i].v;
cout << tr.query(1, 1, n, dfn[po]) << " " << endl;
}
cout << endl;
}
return 0;
}