endless(2018.10.25)
这题就是个线段树合并板子。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int f[400001],n,m,pre[800001],cnt,rt[400001],ls[10000001],rs[10000001],nxt[800001],h[400001],a[400001],b[400001],id,sum[10000001],pw[10000001],ans[400001],ans1[400001];
void add(int x,int y)
{
pre[++cnt]=y;nxt[cnt]=h[x];h[x]=cnt;
pre[++cnt]=x;nxt[cnt]=h[y];h[y]=cnt;
}
void update(int x)
{
if(sum[ls[x]]>sum[rs[x]])sum[x]=sum[ls[x]],pw[x]=pw[ls[x]];
else if(sum[ls[x]]==sum[rs[x]])sum[x]=sum[ls[x]],pw[x]=min(pw[ls[x]],pw[rs[x]]);
else sum[x]=sum[rs[x]],pw[x]=pw[rs[x]];
}
void build(int &k,int l,int r,int v)
{
if(!k)k=++id;
if(l==r){sum[k]+=b[v];pw[k]=a[v];return ;}
int mid=(l+r)>>1;
if(a[v]<=mid)build(ls[k],l,mid,v);
else build(rs[k],mid+1,r,v);
update(k);
}
int merge(int x,int y,int l,int r)
{
if(!x||!y)return x+y;
if(l==r){sum[x]=sum[x]+sum[y];return x;}
ls[x]=merge(ls[x],ls[y],l,(l+r)>>1);
rs[x]=merge(rs[x],rs[y],((l+r)>>1)+1,r);
update(x);return x;
}
void dfs(int x,int fa)
{
for(int i=h[x];i;i=nxt[i])if(pre[i]!=fa)dfs(pre[i],x);
build(rt[x],1,m,x);
for(int i=h[x];i;i=nxt[i])if(pre[i]!=fa)rt[x]=merge(rt[x],rt[pre[i]],1,m);
ans[x]=sum[rt[x]];ans1[x]=pw[rt[x]];
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1,x,y;i<n;i++)scanf("%d%d",&x,&y),add(x,y);
for(int i=1;i<=n;i++)scanf("%d%d",&a[i],&b[i]);
dfs(1,0);for(int i=1;i<=n;i++)printf("%d %d\n",ans1[i],ans[i]);
}