Relief grain
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 3085 Accepted Submission(s): 903
Problem Description
The soil is cracking up because of the drought and the rabbit kingdom is facing a serious famine. The RRC(Rabbit Red Cross) organizes the distribution of relief grain in the disaster area.
We can regard the kingdom as a tree with n nodes and each node stands for a village. The distribution of the relief grain is divided into m phases. For each phases, the RRC will choose a path of the tree and distribute some relief grain of a certain type for every village located in the path.
There are many types of grains. The RRC wants to figure out which type of grain is distributed the most times in every village.
We can regard the kingdom as a tree with n nodes and each node stands for a village. The distribution of the relief grain is divided into m phases. For each phases, the RRC will choose a path of the tree and distribute some relief grain of a certain type for every village located in the path.
There are many types of grains. The RRC wants to figure out which type of grain is distributed the most times in every village.
Input
The input consists of at most 25 test cases.
For each test case, the first line contains two integer n and m indicating the number of villages and the number of phases.
The following n-1 lines describe the tree. Each of the lines contains two integer x and y indicating that there is an edge between the x-th village and the y-th village.
The following m lines describe the phases. Each line contains three integer x, y and z indicating that there is a distribution in the path from x-th village to y-th village with grain of type z. (1 <= n <= 100000, 0 <= m <= 100000, 1 <= x <= n, 1 <= y <= n, 1 <= z <= 100000)
The input ends by n = 0 and m = 0.
For each test case, the first line contains two integer n and m indicating the number of villages and the number of phases.
The following n-1 lines describe the tree. Each of the lines contains two integer x and y indicating that there is an edge between the x-th village and the y-th village.
The following m lines describe the phases. Each line contains three integer x, y and z indicating that there is a distribution in the path from x-th village to y-th village with grain of type z. (1 <= n <= 100000, 0 <= m <= 100000, 1 <= x <= n, 1 <= y <= n, 1 <= z <= 100000)
The input ends by n = 0 and m = 0.
Output
For each test case, output n integers. The i-th integer denotes the type that is distributed the most times in the i-th village. If there are multiple types which have the same times of distribution, output the minimal one. If there is no relief grain in a village, just output 0.
Sample Input
2 41 21 1 11 2 22 2 22 2 15 31 23 13 45 32 3 31 5 23 3 30 0
Sample Output
1223302
题意:现在有n个村庄,n-1条边,使村庄连接成树,m个操作,每次 u到v的最短路上的村庄w种类粮食都增加1,
最后输出每个村庄数量最大的粮食是哪个种类,如果没有粮食输出0
分析:由于m个操作中全是更新,没有查询,那么就可以离线处理,由于每次更新的都是一条重链或是一点(dfs序从小到大),可以像维护前缀和一样,u 该种类粮食数+1,v+1 该种类粮食数-1,到最后一块更新。由于粮食有很多种类,于是需要开个vetcor记录一下这点都进行了 哪些更新
最后从小到大扫过去,线段树维护当前点每个种类的粮食个数,个数最大的是哪个种类,依次输出
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+200;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
struct edge
{
int v,next;
} e[N<<1];
int siz[N],tid[N],rnk[N],top[N],faz[N],son[N],dep[N];
int first[N],tot,cnt,nn,ans[N];;
vector<pair<int,int> >g[N];
struct node
{
int l,r,maxx,id;
int mid()
{
return (l+r)>>1;
}
} t[N<<2];
void add(int u,int v)
{
e[tot].v=v;
e[tot].next=first[u];
first[u]=tot++;
}
void dfs1(int u,int father,int depth)
{
siz[u]=1;
dep[u]=depth;
faz[u]=father;
for(int i=first[u]; ~i; i=e[i].next)
{
int v=e[i].v;
if(v!=father)
{
dfs1(v,u,depth+1);
siz[u]+=siz[v];
if(son[u]==-1||siz[v]>siz[son[u]])
son[u]=v;
}
}
}
void dfs2(int u,int t)
{
tid[u]=++cnt;
rnk[cnt]=u;
top[u]=t;
if(son[u]==-1)
return;
dfs2(son[u],t);
for(int i=first[u]; ~i; i=e[i].next)
{
int v=e[i].v;
if(v!=faz[u]&&v!=son[u])
dfs2(v,v);
}
}
void pushup(int rt)
{
t[rt].maxx=max(t[rt<<1].maxx,t[rt<<1|1].maxx);
if(t[rt<<1].maxx>=t[rt<<1|1].maxx)
t[rt].id=t[rt<<1].id;
else
t[rt].id=t[rt<<1|1].id;
}
void build(int l,int r,int rt)
{
t[rt].l=l,t[rt].r=r;
if(l==r)
{
t[rt].maxx=0;
t[rt].id=l;
return;
}
int m=t[rt].mid();
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
pushup(rt);
}
void update(int q,int rt,int w)
{
if(t[rt].l==t[rt].r)
{
t[rt].maxx+=w;
return;
}
int m=t[rt].mid();
if(q<=m)update(q,rt<<1,w);
else update(q,rt<<1|1,w);
pushup(rt);
}
void update_path(int u,int v,int w)
{
while(top[u]!=top[v])
{
if(dep[top[u]]<dep[top[v]])swap(u,v);
g[tid[top[u]]].push_back(make_pair(w,1));
g[tid[u]+1].push_back(make_pair(w,-1));
u=faz[top[u]];
}
if(tid[u]>tid[v])swap(u,v);
g[tid[u]].push_back(make_pair(w,1));
g[tid[v]+1].push_back(make_pair(w,-1));
}
void init(int n)
{
for(int i=1; i<=n; i++)g[i].clear();
mem(son,-1);
mem(first,-1);
tot=cnt=0;
}
int main()
{
int n,u,v,w,m;
while(scanf("%d%d",&n,&m)&&n+m)
{
nn=0;
init(n);
for(int i=2; i<=n; i++)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs1(1,1,1);
dfs2(1,1);
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
nn=max(nn,w);
update_path(u,v,w);
}
build(1,100000,1);
for(int i=1; i<=n; i++)
{
for(int j=0; j<g[i].size(); j++)
{
update(g[i][j].first,1,g[i][j].second);
}
if(t[1].maxx)
ans[rnk[i]]=t[1].id;
else
ans[rnk[i]]=0;
}
for(int i=1;i<=n;i++)
printf("%d\n",ans[i]);
}
return 0;
}