hdu 5029 Relief grain 树链剖分

Relief grain

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


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}.
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   5041  5040  5039  5038  5036 
 

刚开始实在是想不通100000个节点,每个节点可能有100000个值不知道怎么保存。。

没想到只要想计数的树状数组一样,标记一段区间就加一个然后减一个。。

刚开始看别人题解纠结怎么在一个区间加一个,减一个,这样同样的东西不是覆盖了吗?

原来是先记录所有的边,然后从第一个点开始,每个点的状态用当前的线段树表示。

刚开始的时候糊里糊涂的,难道建100000个线段树?看了kuangbin大神的代码看懂了,原来每次只要update一下就能能到新的线段树。。

好神奇啊~

模仿了一下姿势,dfs树链剖分改成了循环,好像没快多少。

/***********************************************\
 |Author: YMC
 |Created Time: 2014/9/22 21:23:55
 |File Name: hdu 5029 self.cpp
 |Description: 
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;

using namespace std;
#define maxn 100010
vector <int> ma[maxn];
int pathid[maxn],pathtop[maxn],dep[maxn];
int que[maxn*2],s,t,fa[maxn],size[maxn],son[maxn],tot;
int fq[maxn]; 
int n,m,u,v,tp;
inline void add_edge(int a,int b){
    ma[a].push_back(b);ma[b].push_back(a);
}
void buildpath() {
    int u,v;
    s = 0;t = 0;
    que[t ++] = 1;          //注意修改起点
    fa[1] = -1;dep[1] = 1;
    while(s < t) {
        u = que[s ++];
        rep(i,ma[u].size()) {
            v = ma[u][i];
            if(v != fa[u]) {
                fa[v] = u; dep[v] = dep[u] + 1; que[t ++] = v;
            }
        }
    }
    for(int j = n-1;j >= 0;--j) {
        u = que[j];
        son[u] = -1; size[u] = 1;
        rep(i,ma[u].size()) {
            v = ma[u][i];
            if(v != fa[u]) {
                size[u] += size[v];
                if(son[u] == -1 || size[v] > size[son[u]]) son[u] = v;
            }
        }
        if(son[u] == -1) son[u] = u;
    }
    memset(pathtop,-1,sizeof(pathtop));
    int cnt = 1;
    for(int i = 0;i<n;++i) {
        u = que[i];
        if(pathtop[u] != -1) continue;
        int top = u;
        for(;;) {
            pathtop[u] = top;
            pathid[u] = cnt ++;
            fq[pathid[u]] = u;
            if(son[u] == u) break;
            u = son[u];
        }        
    }
}
vector <int> avec[maxn],dvec[maxn];
void change(int u,int v,int z) {
    int f1 = pathtop[u],f2 = pathtop[v];
    while(f1 != f2) {
        if(dep[f1] < dep[f2]) { //始终让f1在下面
            swap(f1,f2);
            swap(u,v);
        }
        avec[pathid[f1]].push_back(z);
        dvec[pathid[u] + 1].push_back(z);
        u = fa[f1];
        f1 = pathtop[u];
    }
    if(dep[u] > dep[v]) swap(u,v);
    avec[pathid[u]].push_back(z);
    dvec[pathid[v] + 1].push_back(z);
}
void init(int n){ 
    rep(i,n+1){
        ma[i].clear();
        avec[i].clear();dvec[i].clear();
    }
    tot = 0;
}
struct Tree{
    int l,r,id,m;
}tree[maxn << 2];
void pushup(int rt) {
    if(tree[L(rt)].m < tree[R(rt)].m) {
        tree[rt].m = tree[R(rt)].m;
        tree[rt].id = tree[R(rt)].id;
    } else {
        tree[rt].m = tree[L(rt)].m;
        tree[rt].id = tree[L(rt)].id;
    }
}
void build(int rt,int l,int r){
    tree[rt].l = l;tree[rt].r = r;
    if(l == r){
        tree[rt].id = l;
        tree[rt].m = 0;
        return ;
    }
    int mid = (l + r) >> 1;
    build(L(rt),l,mid);
    build(R(rt),mid + 1,r);
    pushup(rt);
}
void update(int rt,int u,int v){
    int l = tree[rt].l,r = tree[rt].r;
    if(tree[rt].l == u && tree[rt].r == u){
        tree[rt].m += v;
        return ;
    }
    int mid = (tree[rt].l + tree[rt].r) >> 1;
    if(u <= mid) update(L(rt),u,v);
    else update(R(rt),u,v);
    pushup(rt);
}
int ans[maxn];
int main() {
    //freopen("input.txt","r",stdin); 
    while(~scanf("%d %d",&n,&m)) {
        if(n == 0 && m == 0) break;
        init(n);
        rep(i,n-1) {
            scanf("%d %d",&u,&v);
            add_edge(u,v);
        }
        buildpath();
        rep(i,m){
            scanf("%d %d %d",&u,&v,&tp);
            change(u,v,tp);
        }
        build(1,1,100000);
        for(int i=1;i<=n;++i){
            for(int j=0;j<avec[i].size();++j) {
                update(1,avec[i][j],1);
            }
            for(int j=0;j<dvec[i].size();++j) {
                update(1,dvec[i][j],-1);
            }
            u = fq[i];
            if(tree[1].m == 0) ans[u] = 0;
            else ans[u] = tree[1].id;
        }

        for(int i=1;i<=n;++i) {
            printf("%d\n",ans[i]);
        }
    }
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值