【题目大意】
有一棵树,m次操作。对于一次操作:x y z,意思是对于点x到点y的路径上,每个点都添加种类为z的一个食物。操作完成后,每个点都有若干种食物,每种食物有一定的数量。对于每个点,输出该点数量最多的那种食物,如果有多种,输出种类编号最小的,如果没有,输出0。
【思路】
拿到这种题,想到树链剖分是不难的。
如果直接维护的话,因为食物的种类太多,复杂度显然过高。但是注意到,树链剖分,剖分后,实际上是类似于线段树的维护。我们把操作定在线段的树的节点上,每个点的最后情况,就是线段树的根到对应叶子节点的所有操作和。
举个例子吧。树链剖分后,操作的实质就是对某个区间添加一种食物z,数量为1。最后的答案就是每个点数目最多的食物。我们将操作定在线段树节点上(不做任何下传),如果有5个点,最后点1的的食物情况,就是下图中,黑色线段中所有操作的和。
那么,如果在外部,用另外一个线段树维护食物的情况,扫一下原来的那个线段树,这题就能做了。
但是,很遗憾,这样会T。。。原来一个操作,最多能分成log(n)个连续线段,一个线段,在线段树上最多能分成log(n)个节点。那么,最开始的那个线段树上最多有log(n)^2个操作,外部又用线段树维护,总复杂度最坏为n*log(n)^3。
我们回到,“原题中的一个操作,树剖后,就是对log(n)个线段进行操作”这点。突然想到,如果我们在线段首部添加这个操作,然后在线段末部删掉这种操作,最后扫一下这个整个线段不就好了!这样的话,总操作数会下降到n*log(n)*2。最坏复杂度为n*log(n)^2。
P.S. 赛后发现,这题居然是原题,顿时觉得世界满满的恶意。。。
#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<cctype>
#include<string>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<map>
#include<set>
using namespace std;
#define MP(x,y) make_pair((x),(y))
#define PB(x) push_back(x)
//typedef __int64 LL;
//typedef unsigned __int64 ULL;
/* ****************** */
const int INF = 1000111222;
const double INFF = 1e100;
const double eps = 1e-8;
const int mod = 1000000007;
const int NN = 100010;
const int MM = 400010;
/* ****************** */
vector<int>ad[NN];
vector<int>del[NN];
struct TR
{
int l,r,col,ci;
int mid()
{
return (l+r)>>1;
}
}tr[NN*4];
struct G
{
int v,next;
}E[NN*2];
int p[NN],T;
//c_dfs1
int deep[NN],si[NN],per[NN],anc[NN];
//c_dfs2
int tsp,pos[NN],top[NN];
int ans[NN],dui[NN];
void add(int u,int v)
{
E[T].v=v;
E[T].next=p[u];
p[u]=T++;
}
void c_dfs1(int u,int fa,int cen)
{
anc[u]=fa;
deep[u]=cen;
si[u]=1;
per[u]=-1;
int i,v;
for(i=p[u];i+1;i=E[i].next)
{
v=E[i].v;
if(v==fa)continue;
c_dfs1(v,u,cen+1);
si[u]+=si[v];
if(per[u]==-1 || si[v]>si[ per[u] ])
per[u]=v;
}
}
void c_dfs2(int u,int fa,int now_top)
{
pos[u]=++tsp;
top[u]=now_top;
if(per[u]!=-1)c_dfs2(per[u],u,now_top);
int i,v;
for(i=p[u];i+1;i=E[i].next)
{
v=E[i].v;
if(v==per[u] || v==fa)continue;
c_dfs2(v,u,v);
}
}
void c_update(int u,int v,int col)
{
while(top[u]!=top[v])
{
if(deep[ top[u] ] > deep[ top[v] ])
swap(u,v);
ad[ pos[ top[v] ] ].PB(col);
del[ pos[v] ].PB(col);
v=anc[ top[v] ];
}
if(pos[u]>pos[v])
swap(u,v);
ad[ pos[u] ].PB(col);
del[ pos[v] ].PB(col);
}
void push_up(int R)
{
if(tr[R<<1].ci >= tr[R<<1|1].ci)
{
tr[R].col = tr[R<<1].col;
tr[R].ci = tr[R<<1].ci;
}
else
{
tr[R].col = tr[R<<1|1].col;
tr[R].ci = tr[R<<1|1].ci;
}
}
void build(int l,int r,int R)
{
tr[R].l = l;
tr[R].r = r;
if(l==r)
{
tr[R].col = l;
tr[R].ci = 0;
return;
}
int mid = tr[R].mid();
build(l,mid,R<<1);
build(mid+1,r,R<<1|1);
push_up(R);
}
void update(int x,int R,int ad)
{
if(tr[R].l == tr[R].r)
{
tr[R].ci += ad;
return;
}
int mid = tr[R].mid();
if(x <= mid)
update(x,R<<1,ad);
else
update(x,R<<1|1,ad);
push_up(R);
}
int main()
{
int n,m,u,v,i,c,j,si,s_col;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0 && m==0)break;
memset(p,-1,sizeof(p));
T=0;
for(i=1;i<n;i++)
{
scanf("%d%d", &u, &v);
add(u,v);
add(v,u);
}
c_dfs1(1,-1,0);
tsp = 0;
c_dfs2(1,-1,1);
for(i=1;i<=n;i++)
{
dui[ pos[i] ] = i;
}
s_col = 1;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&c);
s_col = max(s_col,c);
c_update(u,v,c);
}
build(0,s_col,1);
for(i=1;i<=n;i++)
{
si = ad[i].size();
for(j=0;j<si;j++)
{
update(ad[i][j], 1, 1);
}
ans[ dui[i] ] = tr[1].col;
si = del[i].size();
for(j=0;j<si;j++)
{
update(del[i][j], 1, -1);
}
ad[i].clear();
del[i].clear();
}
for(i=1;i<=n;i++)
{
printf("%d\n",ans[i]);
}
}
return 0;
}