首页 > 代码库 > HDU 5029 树链剖分

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


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

代码;

HDU 5029 树链剖分