Relief grain
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=5029
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.
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.
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 4
1 2
1 1 1
1 2 2
2 2 2
2 2 1
5 3
1 2
3 1
3 4
5 3
2 3 3
1 5 2
3 3 3
0 0
Sample Output
1
2
2
3
3
0
2
HINT
题意
有n个点的树,有m次操作
操作是在x-y的链上的每一个点,都增加一个W
然后让你输出每个点出现次数最多的数是什么
题解:
看到链状,很显然就是树链剖分
我们对于每个询问都按照lca去打上标记,就G[u].push_back(v,1),G[v].push_back(v,-1)这种
打完标记之后,我们再按照树链剖分的顺序去处理这些标记就好了
代码:
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <vector>
using namespace std;
const int N=;
const int INF=<<; int n,tim;
vector<pair<int,int> >G[];
long long num[N];
int siz[N],top[N],son[N];
int dep[N],tid[N],Rank[N],fa[N];
int head[N],to[*N],Next[*N],edge;
int flag = ;
struct Edge
{
int u,v;
};
Edge tmp[*N];
int ans[N];
void Init()
{
memset(head,-,sizeof(head));
memset(son,-,sizeof(son));
tim=;
edge=;
for(int i=;i<;i++)
G[i].clear();
memset(ans,,sizeof(ans));
} void addedge(int u,int v)
{
to[edge]=v,Next[edge]=head[u],head[u]=edge++;
to[edge]=u,Next[edge]=head[v],head[v]=edge++;
} //树链剖分部分
void dfs1(int u,int father,int d)
{
dep[u]=d;
fa[u]=father;
siz[u]=;
for(int i=head[u]; ~i; i=Next[i])
{
int v=to[i];
if(v!=father)
{
dfs1(v,u,d+);
siz[u]+=siz[v];
if(son[u]==-||siz[v]>siz[son[u]])
son[u]=v;
}
}
} void dfs2(int u,int tp)
{
top[u]=tp;
tid[u]=++tim;
Rank[tid[u]]=u;
if(son[u]==-) return;
dfs2(son[u],tp);
for(int i=head[u]; ~i; i=Next[i])
{
int v=to[i];
if(v!=son[u]&&v!=fa[u])
dfs2(v,v);
}
} void Do(int l,int r,int x)
{
//cout<<l<<" "<<r<<endl;
G[l].push_back(make_pair(x,));
G[r].push_back(make_pair(x,-));
} void solve(int x,int y,int val)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
Do(top[x],x,val);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
Do(x,y,val);
}
/////////////////////////////////////////////////////////线段树 typedef int SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType Max1 , lazy ,Max2;
}; treenode tree[N]; inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R,tree[o].Max1 = ;
tree[o].Max2 = L;
if (R > L)
{
int mid = (L+R) >> ;
build_tree(L,mid,o*);
build_tree(mid+,R,o*+);
}
} inline void updata(int QL,int QR,SgTreeDataType v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR)
tree[o].Max1 += v;
else
{
int mid = (L+R)>>;
if (QL <= mid) updata(QL,QR,v,o*);
if (QR > mid) updata(QL,QR,v,o*+);
if(tree[o*].Max1>=tree[o*+].Max1)
tree[o].Max1 = tree[o*].Max1,tree[o].Max2 = tree[o*].Max2;
else
tree[o].Max1 = tree[o*+].Max1,tree[o].Max2 = tree[o*+].Max2;
}
} ///////////////////////////////////////////////////
int MM;
void Get(int u,int tp)
{
//cout<<u<<endl;
for(int j=;j<G[u].size();j++)
{
if(G[u][j].second==-)continue;
//cout<<G[u][j].first<<" "<<G[u][j].second<<endl;
updata(G[u][j].first,G[u][j].first,G[u][j].second,);
}
//cout<<u<<" "<<tree[1].Max2<<" flag"<<endl;
ans[u]=tree[].Max2;
for(int j=;j<G[u].size();j++)
{
if(G[u][j].second==)continue;
//cout<<G[u][j].first<<" "<<G[u][j].second<<endl;
updata(G[u][j].first,G[u][j].first,G[u][j].second,);
}
if(son[u]==-) return;
Get(son[u],tp);
for(int i=head[u]; ~i; i=Next[i])
{
int v=to[i];
if(v!=son[u]&&v!=fa[u])
Get(v,v);
}
}
int main()
{
while(scanf("%d%d",&n,&MM)!=EOF)
{
if(n==&&MM==)break;
Init();
for(int i=; i<n; i++)
{
int a,b;
scanf("%d%d",&a,&b);
addedge(a,b);
}
dfs1(,,);
dfs2(,);
while(MM--)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
solve(x,y,z);
}
build_tree(,,);
Get(,);
for(int i=;i<=n;i++)
printf("%d\n",ans[i]);
}
return ;
}