HDU 5029 树链剖分

Relief grain

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)

Total Submission(s): 861    Accepted Submission(s): 219

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 :rabbit
Created Time :2014/10/5 10:18:36
File Name :7.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=100100;
int head[maxn],tol,n,m,dep[maxn],son[maxn],top[maxn],que[maxn],fa[maxn][19];
int sum[maxn],cnt[maxn],ans[maxn];
struct Edge{
    int next,to;
    Edge(int _next=0,int _to=0){
        next=_next;to=_to;
    }
}edge[200010];
void addedge(int u,int v){
    edge[tol]=Edge(head[u],v);
    head[u]=tol++;
}
void divide(){
    memset(sum,0,sizeof(sum));
    memset(son,0,sizeof(son));
    memset(top,0,sizeof(top));
    int front=1,rear=1;
    que[rear++]=1;dep[1]=0;fa[1][0]=0;
    while(front!=rear){
        int now=que[front++];
        for(int i=1;i<19;i++)fa[now][i]=fa[fa[now][i-1]][i-1];
        for(int i=head[now];i!=-1;i=edge[i].next){
            int v=edge[i].to;
            if(v==fa[now][0])continue;
            dep[v]=dep[now]+1;
            fa[v][0]=now;
            que[rear++]=v;
        }
    }
    for(int i=n;i>=1;i--)sum[fa[que[i]][0]]+=++sum[que[i]];
    for(int i=1;i<=n;i++){
        int u=que[i],best=0;
        if(top[u]==0)top[u]=u;
        for(int j=head[u];j!=-1;j=edge[j].next){
            int v=edge[j].to;
            if(v==fa[u][0])continue;
            if(sum[v]>best)son[u]=v,best=sum[v];
        }
		top[son[u]]=top[u];
    }
}
int find(int x,int d){
    for(int i=18;i>=0;i--)if(d&(1<<i))x=fa[x][i];
    return x;
}
int LCA(int x,int y){
    while(1){
        if(dep[x]>dep[y])swap(x,y);
        if(top[x]==top[y])return x;
        if(dep[top[x]]<dep[top[y]])y=fa[top[y]][0];
        else x=fa[top[x]][0];
    }
}
int lca(int x,int y){
    if(dep[x]<dep[y])swap(x,y);
    for(int i=18;i>=0;i--)if((dep[x]-dep[y])&(1<<i))x=fa[x][i];
    if(x==y)return x;
    for(int i=18;i>=0;i--)if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
    return fa[x][0];
}
vector<pair<int,int> > c[maxn];
void insert(int x,int y,int z){
    while(top[x]!=top[y]){
        c[top[y]].push_back(make_pair(z,0));
        c[y].push_back(make_pair(z,1));
        y=fa[top[y]][0];
    }
    c[x].push_back(make_pair(z,0));
    c[y].push_back(make_pair(z,1));
}
int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
     while(~scanf("%d%d",&n,&m)){
         if(n==0&&m==0)break;
         memset(head,-1,sizeof(head));tol=0;
         for(int i=1;i<n;i++){
             int x,y;
             scanf("%d%d",&x,&y);
             addedge(x,y);
             addedge(y,x);
         }
         for(int i=0;i<maxn;i++)c[i].clear();
         divide();
         for(int i=1;i<=m;i++){
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            if(dep[x]>dep[y])swap(x,y);
            int A=lca(x,y);
            if(A!=x)insert(find(x,dep[x]-dep[A]-1),x,z);
            insert(A,y,z);
        }
        // cout<<"ddd "<<endl;
         priority_queue<pair<int,int> > Q;
         memset(cnt,0, sizeof(cnt));
         memset(ans,0,sizeof(ans));
         for(int i=1;i<=n;i++)
             if(top[i]==i){
                 while(!Q.empty())Q.pop();
                 for(int u=i;u;u=son[u]){
                     vector<pair<int,int> >::iterator it;
                     for(it=c[u].begin();it!=c[u].end();it++)
                         if(it->second==0)Q.push(make_pair(++cnt[it->first],-it->first));
                     while(!Q.empty()&&Q.top().first!=cnt[-Q.top().second])Q.pop();
                     if(!Q.empty())ans[u]=-Q.top().second;
                     for(it=c[u].begin();it!=c[u].end();it++)
                         if(it->second==1)if(--cnt[it->first])Q.push(make_pair(cnt[it->first],-it->first));
                 }
             }
         for(int i=1;i<=n;i++)printf("%d\n",ans[i]);
     }
     return 0;
}

非递归的树链剖分简直炫酷。

代码;

时间: 2024-10-08 10:16:22

HDU 5029 树链剖分的相关文章

hdu 5893 (树链剖分+合并)

List wants to travel Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 429    Accepted Submission(s): 92 Problem Description A boy named List who is perfect in English. Now he wants to travel an

hdu 5052 树链剖分

Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 982    Accepted Submission(s): 274 Problem Description Yaoge likes to eat chicken chops late at night. Yaoge has eaten too

hdu 5242 树链剖分找权值最大的前k条链

http://acm.hdu.edu.cn/showproblem.php?pid=5242 Problem Description It is well known that Keima Katsuragi is The Capturing God because of his exceptional skills and experience in ''capturing'' virtual girls in gal games. He is able to play k games sim

hdu 5274 树链剖分

Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1484    Accepted Submission(s): 347 Problem Description Dylans is given a tree with N nodes. All nodes have a value A[i].Nodes

hdu 4897 树链剖分(重轻链)

Little Devil I Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 894    Accepted Submission(s): 296 Problem Description There is an old country and the king fell in love with a devil. The devil

HDU 3966 (树链剖分+线段树)

Problem Aragorn's Story (HDU 3966) 题目大意 给定一颗树,有点权. 要求支持两种操作,将一条路径上的所有点权值增加或减少ai,询问某点的权值. 解题分析 树链剖分模板题. 实质上树链剖分进行了点对点的一次映射,保证了重链上的点在线段树上的位置是连续的. 树链剖分的两个性质(转): 性质1:如果(v,u)为轻边,则siz[u] * 2 < siz[v]: 性质2:从根到某一点的路径上轻链.重链的个数都不大于logn. 保证了一个区间的时间复杂度是log2(n).

HDU 5274(树链剖分)

树链剖分第一题QAQ,纪念下 #pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef long long ll; const ll mod = 1e9 + 7; const int maxn = 1e5 + 10; #define

hdu 3804树链剖分+离线操作

/* 树链刨分+离线操作 题意:给你一棵树,和询问x,y 从节点x--节点1的小于等于y的最大值. 解:先建一个空树,将树的边权值从小到大排序,将询问y按从小到大排序 对于每次询问y将小于等于y的边权值的边加入,在进行询问将结果储存最后输出即可 易错点:要考虑到节点1到节点1的情况需特判. */ #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #include<string

HDU 5405 (树链剖分+线段树)

Problem Sometimes Naive 题目大意 给你一棵n个节点的树,有点权. 要求支持两种操作: 操作1:更改某个节点的权值. 操作2:给定u,v, 求 Σw[i][j]   i , j 为任意两点且i到j的路径与u到v的路径相交. 解题分析 容易发现对于一个询问,答案为总点权和的平方 减去 去掉u--v这条链后各个子树的点权和的平方的和. 开两棵线段树,tag1记录点权和,tag2记录某点的所有轻链子树的点权和的平方的和. 每次沿着重链往上走时,直接加上这条重链的所有点的tag2和