Description
小c同学认为跑步非常有趣,于是决定制作一款叫做《天天爱跑步》的游戏。?天天爱跑步?是一个养成类游戏,需要
玩家每天按时上线,完成打卡任务。这个游戏的地图可以看作一一棵包含 N个结点和N-1 条边的树, 每条边连接两
个结点,且任意两个结点存在一条路径互相可达。树上结点编号为从1到N的连续正整数。现在有个玩家,第个玩家的
起点为Si ,终点为Ti 。每天打卡任务开始时,所有玩家在第0秒同时从自己的起点出发, 以每秒跑一条边的速度,
不间断地沿着最短路径向着自己的终点跑去, 跑到终点后该玩家就算完成了打卡任务。 (由于地图是一棵树, 所以
每个人的路径是唯一的)小C想知道游戏的活跃度, 所以在每个结点上都放置了一个观察员。 在结点的观察员会选
择在第Wj秒观察玩家, 一个玩家能被这个观察员观察到当且仅当该玩家在第Wj秒也理到达了结点J 。 小C想知道
每个观察员会观察到多少人?注意: 我们认为一个玩家到达自己的终点后该玩家就会结束游戏, 他不能等待一 段时
间后再被观察员观察到。 即对于把结点J作为终点的玩家: 若他在第Wj秒重到达终点,则在结点J的观察员不能观察
到该玩家;若他正好在第Wj秒到达终点,则在结点的观察员可以观察到这个玩家。
Input
第一行有两个整数N和M 。其中N代表树的结点数量, 同时也是观察员的数量, M代表玩家的数量。
接下来n-1 行每行两个整数U和V ,表示结点U 到结点V 有一条边。
接下来一行N 个整数,其中第个整数为Wj , 表示结点出现观察员的时间。
接下来 M行,每行两个整数Si和Ti,表示一个玩家的起点和终点。
对于所有的数据,保证 。
1<=Si,Ti<=N,0<=Wj<=N
Output
输出1行N 个整数,第个整数表示结点的观察员可以观察到多少人。
Sample Input
6 3
2 3
1 2
1 4
4 5
4 6
0 2 5 1 2 3
1 5
1 3
2 6
Sample Output
1 2 1 0 1
HINT
对于1号点,Wi=0,故只有起点为1号点的玩家才会被观察到,所以玩家1和玩家2被观察到,共有2人被观察到。
对于2号点,没有玩家在第2秒时在此结点,共0人被观察到。
对于3号点,没有玩家在第5秒时在此结点,共0人被观察到。
对于4号点,玩家1被观察到,共1人被观察到。
对于5号点,玩家1被观察到,共1人被观察到。
对于6号点,玩家3被观察到,共1人被观察到。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define N 300005
using namespace std;
int n,m,cnt,tot,last[N],ls[N],ans[N],tim[N],fa[N][30],dep[N],sz,root1[N],root2[N];
struct edge { int to,next; } e[N<<1];
struct query { int dep,val,op,next; } q[N<<2];
struct tree { int l,r,s; } t[N*120];
template <class T> inline void read(T &x) {
int flag = 1; x = 0;
char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') flag = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9') { x = (x<<1)+(x<<3)+ch-'0'; ch = getchar(); }
x *= flag;
}
void addedge(int u, int v) {
e[++cnt].to = v; e[cnt].next = last[u]; last[u] = cnt;
e[++cnt].to = u; e[cnt].next = last[v]; last[v] = cnt;
}
void ins(int x, int dep, int op, int val) {
q[++tot].dep = dep; q[tot].op = op; q[tot].val = val; q[tot].next = ls[x]; ls[x] = tot;
}
void dfs1(int x) {
dep[x] = dep[fa[x][0]]+1;
for(int i = 1; i <= 20; i++) fa[x][i] = fa[fa[x][i-1]][i-1];
for(int i = last[x]; i; i = e[i].next) {
if(e[i].to == fa[x][0]) continue;
fa[e[i].to][0] = x;
dfs1(e[i].to);
}
}
int getlca(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
for(int i = 20; i >= 0; i--) if(dep[fa[x][i]] >= dep[y]) x = fa[x][i];
if(x == y) return x;
for(int i = 20; i >= 0; i--) if(fa[x][i]!=fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
int getson(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
for(int i = 20; i >= 0; i--) if(dep[fa[x][i]] > dep[y]) x = fa[x][i];
return x;
}
void ins(int &d, int l, int r, int x, int y) {
if(!d) d = ++sz;
t[d].s += y;
if(l == r) return;
int mid = (l+r)>>1;
if(x <= mid) ins(t[d].l, l, mid, x, y);
else ins(t[d].r, mid+1, r, x, y);
}
int find(int d, int l, int r, int x) {
if(l == r || !d) return t[d].s;
int mid = (l+r)>>1;
if(x <= mid) return find(t[d].l, l, mid, x);
else return find(t[d].r, mid+1, r, x);
}
int merge(int x, int y) {
if(!x) return y;
if(!y) return x;
t[x].s += t[y].s;
t[x].l = merge(t[x].l, t[y].l);
t[x].r = merge(t[x].r, t[y].r);
return x;
}
void dfs2(int x) {
for(int i = last[x]; i; i = e[i].next) {
if (e[i].to == fa[x][0]) continue;
dfs2(e[i].to);
root1[x] = merge(root1[x], root1[e[i].to]);
root2[x] = merge(root2[x], root2[e[i].to]);
}
for(int i = ls[x]; i; i = q[i].next) if(q[i].val == 1) {
if(q[i].op == 1) ins(root1[x], 1, n*3, q[i].dep, 1);
else ins(root2[x], 1, n*3, q[i].dep, 1);
}
ans[x] = find(root1[x], 1, n*3, dep[x]+tim[x]+n)+find(root2[x], 1, n*3, dep[x]-tim[x]+n);
for(int i = ls[x]; i; i = q[i].next) if(q[i].val == -1) {
if(q[i].op == 1) ins(root1[x], 1, n*3, q[i].dep, -1);
else ins(root2[x], 1, n*3, q[i].dep, -1);
}
}
int main() {
read(n); read(m);
int x,y;
for(int i = 1; i < n; i++) read(x), read(y), addedge(x, y);
dfs1(1);
for(int i = 1; i <= n; i++) read(tim[i]);
for(int i = 1; i <= m; i++) {
int x,y,lca;
read(x), read(y), lca = getlca(x, y);
ins(x, dep[x]+n, 1, 1);
ins(lca, dep[x]+n, 1, -1);
if(y != lca) {
int sy = getson(lca, y);
ins(y, dep[y]-dep[x]-dep[y]+dep[lca]*2+n, 2, 1);
ins(sy, dep[y]-dep[x]-dep[y]+dep[lca]*2+n, 2, -1);
}
}
dfs2(1);
for(int i = 1; i < n; i++) printf("%d ",ans[i]);
printf("%d",ans[n]);
return 0;
}