啊哈~
【题目分析】
哇这真的是DAY1T2吗。。。。。感觉要被强行劝退了。。。。。qwq
首先对于树上的路径s->t,我们都可以将其拆为s->lca(s,t)->t,我们用dona数组作为记录贡献的桶。
考虑一个点i能被观察到的情况,分为两种:
1.存在一条路径s->lca(s,t),且depth[i]+val[i](即原题的w)=depth[s]
2.存在一条路径lca(s,t)->t,且depth[i]-val[i]=depth[t]-lenth[s,t](lenth数组记录s->t的路径长度)
所以我们考虑记录两个贡献:一个点在s->lca(s,t)上的贡献和lca(s,t)->t上的贡献,在dfs的时候我们记录dfs前和dfs后的差值,就是最后该点的贡献。(在代码中用1~MAXN维护第一个,MAXN+1~MAXN*2维护第二个)
对于树上的节点,维护三个信息:以该点为终点的路径编号,以该点为lca的路径的编号,以该点为起点的次数,用前两者统计对祖先的贡献,后者统计不会做出贡献的点,注意有可能lca会被重复计算,所以要在处理路径信息的时候提前减去。
反正写的很难受qwq,NOIP好难好难qwq
【代码~】
#include<bits/stdc++.h>
using namespace std;
const int MAXN=6e5+10;
const int MAXM=1e6+21000;int n,m,cnt,s[MAXN],t[MAXN],lc[MAXN],lenth[MAXN];
int head[MAXN],depth[MAXN],fa[MAXN],son[MAXN],siz[MAXN],top[MAXN],val[MAXN],c[MAXN];
int nxt[MAXM],to[MAXM];
int start[MAXN],dona[MAXM<<1],a[MAXN],len[MAXN],ans[MAXN];
vector<int> anc[MAXN],end[MAXN];int Read()
{int i=0,f=1;char c;for(c=getchar();(c>'9'||c<'0')&&c!='-';c=getchar());if(c=='-')f=-1,c=getchar();for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';return i*f;
}void add(int x,int y)
{cnt++;nxt[cnt]=head[x];head[x]=cnt;to[cnt]=y;
}void dfs1(int u,int fat)
{siz[u]=1,fa[u]=fat,depth[u]=depth[fat]+1;for(int i=head[u];i;i=nxt[i]){int v=to[i];if(v!=fa[u]){c[v]=c[u]+1;dfs1(v,u);siz[u]+=siz[v];if(siz[son[u]]<siz[v])son[u]=v;}}
}void dfs2(int u,int tp)
{top[u]=tp;if(!son[u])return ;dfs2(son[u],tp);for(int i=head[u];i;i=nxt[i]){int v=to[i];if(v!=son[u]&&v!=fa[u]){dfs2(v,v);}}
}int lca(int x,int y)
{while(top[x]!=top[y]){if(depth[top[x]]<depth[top[y]])swap(x,y);x=fa[top[x]];}return depth[x]<depth[y]?x:y;
}void dfs3(int u)
{int xx=dona[depth[u]+val[u]],yy=dona[val[u]-depth[u]+MAXN]; for(int i=head[u];i;i=nxt[i]){int v=to[i];if(v==fa[u]) continue;dfs3(v);}dona[depth[u]]+=start[u];for(int i=0;i<end[u].size();i++){int v=end[u][i];dona[lenth[v]-depth[t[v]]+MAXN]++;}ans[u]+=dona[depth[u]+val[u]]-xx+dona[val[u]-depth[u]+MAXN]-yy;for(int i=0;i<anc[u].size();i++){int v=anc[u][i];dona[depth[s[v]]]--,dona[lenth[v]-depth[t[v]]+MAXN]--;}return;
}int main()
{n=Read(),m=Read();for(int i=1;i<n;++i){int x=Read(),y=Read();add(x,y),add(y,x);}for(int i=1;i<=n;++i)val[i]=Read();dfs1(1,0);dfs2(1,1);for(int i=1;i<=m;++i){s[i]=Read(),t[i]=Read();lc[i]=lca(s[i],t[i]);lenth[i]=c[s[i]]+c[t[i]]-c[lc[i]]*2;anc[lc[i]].push_back(i);end[t[i]].push_back(i);start[s[i]]++;if(depth[s[i]]==depth[lc[i]]+val[lc[i]])ans[lc[i]]--;}dfs3(1);for(int i=1;i<=n;++i)cout<<ans[i]<<' ';return 0;
}