HDU 5029 Relief grain (树链剖分+线段树)

HDU5029 题意很简单。 做法就是进行树链剖分,这样就可以把树形转线性。 然后对区间[l,r] 加z, 可以在l加一个z, 在r+1减掉一个 z. 最后按照fp数组进行扫描一遍,用线段树进行更新,找最大值。 水题!

Relief grain

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 237 Accepted Submission(s): 41

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.

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

For the first test case, the relief grain in the 1st village is {1, 2}, and the relief grain in the 2nd village is {1, 2, 2}.

/* ***
Author :kuangbin
Created Time :2014/9/21 8:39:49
File Name :HDU5029.cpp
************************************************ */

#include <stdio.h>

#include <string.h>

#include

#include

#include

#include

#include

#include

#include

#include <math.h>

#include <stdlib.h>

#include <time.h>
using namespace std;
const int MAXN = 100010;
struct Edge{
int to,next;
}edge[MAXN2];
int head[MAXN],tot;
int top[MAXN];
int fa[MAXN];
int deep[MAXN];
int num[MAXN];
int p[MAXN];
int fp[MAXN];
int son[MAXN];
int pos;
void init(){
tot = 0;
memset(head,-1,sizeof(head));
pos = 1;
memset(son,-1,sizeof(son));
}
void addedge(int u,int v){
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void dfs1(int u,int pre,int d){
deep[u] = d;
fa[u] = pre;
num[u] = 1;
for(int i = head[u];i != -1;i = edge[i].next){
int v = edge[i].to;
if(v != pre){
dfs1(v,u,d+1);
num[u] += num[v];
if(son[u] == -1 || num[v] > num[son[u]])
son[u] = v;
}
}
}
void getpos(int u,int sp){
top[u] = sp;
p[u] = pos++;
fp[p[u]] = u;
if(son[u] == -1)return;
getpos(son[u],sp);
for(int i = head[u];i != -1;i = edge[i].next){
int v = edge[i].to;
if(v != son[u] && v != fa[u])
getpos(v,v);
}
}
vectoravec[MAXN],dvec[MAXN];
void change(int u,int v,int z){
int f1 = top[u],f2 = top[v];
int tmp = 0;
while(f1 != f2){
if(deep[f1] < deep[f2]){
swap(f1,f2);
swap(u,v);
}
avec[p[f1]].push_back(z);
dvec[p[u]+1].push_back(z);
u = fa[f1];
f1 = top[u];
}
if(deep[u] > deep[v])swap(u,v);
avec[p[u]].push_back(z);
dvec[p[v]+1].push_back(z);
}
struct Node{
int l,r;
int Max,id;
void output(){
printf(“l %d r %d Max %d id %d\n”,l,r,Max,id);
}
}segTree[MAXN<<2];
void debug(int i){
segTree[i].output();
if(segTree[i].l == segTree[i].r)return;
debug(i<<1);
debug((i<<1)|1);
}
void push_up(int i){
if(segTree[i<<1].Max < segTree[(i<<1)|1].Max){
segTree[i].Max = segTree[(i<<1)|1].Max;
segTree[i].id = segTree[(i<<1)|1].id;
}
else{
segTree[i].Max = segTree[i<<1].Max;
segTree[i].id = segTree[i<<1].id;
}
}
void build(int i,int l,int r){
segTree[i].l = l;
segTree[i].r = r;
if(l == r){
segTree[i].id = l;
segTree[i].Max = 0;
return;
}
int mid = (l+r)/2;
build(i<<1,l,mid);
build((i<<1)|1,mid+1,r);
push_up(i);
}
void update(int i,int k,int val){
if(segTree[i].l == k && segTree[i].r == k){
segTree[i].Max += val;
return;
}
int mid = (segTree[i].l+segTree[i].r)/2;
if(k <= mid)update(i<<1,k,val);
else update((i<<1)|1,k,val);
push_up(i);
}
int ans[MAXN];
int main()
{
//freopen(“in.txt”,”r”,stdin);
//freopen(“out.txt”,”w”,stdout);
int n,m;
while(scanf(“%d%d”,&n,&m) == 2){
if(n == 0 && m == 0)break;
init();
int u,v;
for(int i = 1;i < n;i++){
scanf(“%d%d”,&u,&v);
addedge(u,v);
addedge(v,u);
}
dfs1(1,0,0);
getpos(1,1);
for(int i = 1;i <= 100000;i++){
avec[i].clear();
dvec[i].clear();
}
int z;
while(m–){
scanf(“%d%d%d”,&u,&v,&z);
change(u,v,z);
}
build(1,1,100000);
for(int i = 1;i <= n;i++){
//debug(1);
//printf(“%d %d\n”,avec[i].size(),dvec[i].size());
for(int j = 0;j < avec[i].size();j++){
update(1,avec[i][j],1);
//printf(“%d
\n”,avec[i][j]);
}
for(int j = 0;j < dvec[i].size();j++){
update(1,dvec[i][j],-1);
//printf(“%d **\n”,dvec[i][j]);
}
u = fp[i];
//printf(“i %d %d\n”,i,fp[i]);
if(segTree[1].Max == 0)ans[u] = 0;
else ans[u] = segTree[1].id;
}
for(int i = 1;i <= n;i++)
printf(“%d\n”,ans[i]);
}
return 0;
}

------ 本文结束------
  • 本文作者: kuangbin
  • 本文链接: 400.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
0%