题目链接
比较裸的树上点差分,需要注意的是每次差分的终点作为上次的起点会重复计算,因此除了第一个点都需要减一。
AC代码:
struct node
{
int v, next;
} e[600010];
int ct = 1;
int head[300010];
void add(int u, int v)
{
e[ct].v = v;
e[ct].next = head[u];
head[u] = ct++;
}
int a[300010];
int father[300010][32];
int depth[300010];
int ans[300010];
void dfs1(int now, int fa, int dep)
{
father[now][0] = fa;
depth[now] = dep;
for (int i = 1; i <= 30; i++)
father[now][i] = father[father[now][i - 1]][i - 1];
for (int i = head[now]; i; i = e[i].next)
if (e[i].v != fa)
dfs1(e[i].v, now, dep + 1);
}
int lca(int u, int v)
{
if (depth[v] > depth[u])
swap(u, v);
int cha = depth[u] - depth[v];
for (int i = 0; i <= 30; i++)
if ((1 << i) & cha)
u = father[u][i];
if (u == v)
return u;
for (int i = 30; i >= 0; i--)
{
if (father[u][i] != father[v][i])
{
u = father[u][i];
v = father[v][i];
}
}
return father[u][0];
}
void cf(int u, int v)
{
int fa = lca(u, v);
ans[fa]--;
ans[father[fa][0]]--;
ans[u]++;
ans[v]++;
}
int dfs2(int now, int fa)
{
for (int i = head[now]; i; i = e[i].next)
if (e[i].v != fa)
ans[now] += dfs2(e[i].v, now);
return ans[now];
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i < n; i++)
{
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
dfs1(1, 0, 1);
for (int i = 2; i <= n; i++)
cf(a[i], a[i - 1]);
dfs2(1, 0);
ans[a[1]]++;
for (int i = 1; i <= n; i++)
printf("%d\n", ans[i] - 1);
return 0;
}