www2003 @ 2019-08-10 18:05:16
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<sstream>
#include<iomanip>
#define pb push_back
#define qr read()
#define LL long long
#define ff first
#define ss second
#define in inline
#define reg register
#define pa pair<int,int>
#define mp make_pair<int,int>
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define per(i,a,b) for(int i = (a); i >= (b); i--)
using namespace std;
const int MAXN=0x3f3f3f3f;
const int MAXL=0x7ffff;
in int read(){
int x = 0,f = 1;
char c=getchar();if(c == '-')f = -1;
while(c>'9'||c<'0')c=getchar();
while(c<='9'&&c>='0')
{
x=x*10+c-'0';
c=getchar();
}
return x * f;
}
inline void write(int re)
{ /*
if (re<0)
{
putchar('-');
re=-re;
}
*/
if (re>9) write(re/10);
putchar(re%10+'0');
}
#define N 1000000
#define SIZE 1000000
int n,m;
int c[N],c1[N],c2[N],to[N],to1[N],to2[N],nxt1[N],nxt2[N],nxt[N];
int b1[N],b2[N * 2],fa[N],dep[N],heavy[N],top[N],Size[N];
int lca[N],s[N],t[N],dis[N];
int w[N],js[N],ans[N];
int cnt1,cnt2,cnt;
inline void add(int u,int v)
{
cnt++;
to[cnt] = v;
nxt[cnt] = c[u];
c[u] = cnt;
}
inline void add1(int u,int v)
{
cnt1++;
to1[cnt1] = v;
nxt1[cnt] = c1[u];
c1[u] = cnt1;
}
inline void add2(int u,int v)
{
cnt2++;
to2[cnt2] = v;
nxt2[cnt2] = c2[u];
c2[u] = cnt2;
}
void dfs1(int now,int Fa)
{
fa[now] = Fa;
dep[now] = dep[Fa] + 1;
Size[now] = 1;
for(int i = c[now]; i ; i = nxt[i])
{
int v = to[i];
if(v == Fa)continue;
dfs1(v,now);
Size[now] += Size[v];
if(!heavy[now] || Size[v] > Size[heavy[now]])heavy[now] = v;
}
}
void dfs2(int now,int Top)
{
top[now] = Top;
if(!heavy[now])return;
dfs2(heavy[now],Top);
for(int i = c[now]; i;i = nxt[i])
{
int v = to[i];
if(v == fa[now] || v == heavy[now])continue;
dfs2(v,v);
}
}
int LCA(int u,int v)
{
int fu = top[u],fv = top[v];
while(fu != fv)
{
if(dep[fu] < dep[fv])
{
swap(fu,fv);
swap(u,v);
}
u = fa[fu];
fu = top[u];
}
if(dep[u] > dep[v])swap(u,v);
return u;
}
void dfs(int x)
{
int t1 = b1[w[x] + dep[x]],t2 = b2[w[x] - dep[x] + SIZE];
for(int i = c[x]; i ; i = nxt[i])
{
int v = to[i];
if(v == fa[x])continue;
dfs(v);
}
b1[dep[x]] += js[x];
for(int i = c1[x]; i; i = nxt1[i])
{
int y = to1[i];
b2[dis[y] - dep[t[y]] + SIZE]++;
}
ans[x] += b1[w[x] + dep[x]] + b2[w[x] - dep[x] + SIZE] - t1 - t2;
for(int i = c2[x]; i;i = nxt2[i])
{
int y = to2[i];
b1[dep[s[y]]]--;
b2[dis[y] - dep[t[y]] + SIZE]--;
}
}
int main()
{
n = qr,m = qr;
rep(i,2,n)
{
int u = qr,v = qr;
add(u,v);
add(v,u);
}
dfs1(1,1);
dfs2(1,1);
for(int i = 1; i <= n; i++)w[i] = qr;
for(int i = 1; i <= m; i++)
{
s[i] = qr,t[i] = qr;
lca[i] = LCA(s[i],t[i]);
dis[i] = dep[s[i]] + dep[t[i]] - 2 * dep[lca[i]];
js[s[i]]++;
add1(t[i],i);
add2(lca[i],i);
if(dep[lca[i]] + w[lca[i]] == dep[s[i]])ans[lca[i]]--;
}
dfs(1);
rep(i,1,n)printf("%d ",ans[i]);
return 0;
}
by 中二病 @ 2019-08-10 18:33:07
%%%
by www2003 @ 2019-08-10 19:04:05
把cnt1 打成cnt了。。。