HDU 5029 Relief grain(树链剖分)

题目链接~~>

做题感悟:这题真的很巧妙,分析了一下午才分析懂其中的奥妙,感觉收获很大。

解题思路:

首先需要树链剖分一下,把树剖分成链。然后的思想和HDU 5044 差不多,只不过这个不用数组遍历,而是用线段树代替数组。如果你在[ a ,b ] 区间染色,则可以让 a 节点加上这种颜色,让 b + 1 减去这种颜色,这样最后遍历一下数组就可以了,但是这题每个节点可以染许多颜色,所以不可以和数组一样只遍历一次,我们可以把那些点弄到线段树上,这样只需要在线段树上更新点就可以了 ,每一次得到的颜色一定是线段树根节点的颜色。

代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<sstream>
#include<map>
#include<cmath>
#include<fstream>
#include<queue>
#include<vector>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<stack>
#include<bitset>
#include<ctime>
#include<string>
#include<cctype>
#include<iomanip>
#include<algorithm>
using namespace std  ;
#define INT  __int64
#define L(x)  (x * 2)
#define R(x)  (x * 2 + 1)
const int INF = 0x3f3f3f3f ;
const double esp = 0.0000000001 ;
const double PI = acos(-1.0) ;
const INT mod = 1000000007 ;
const int MY = 1400 + 5 ;
const int MX = 100000 + 5 ;
int n ,m ,num ,idx ,maxn ;
int head[MX] ,father[MX] ,ti[MX] ,top[MX] ,dep[MX] ,siz[MX] ,son[MX] ,ans[MX] ,P[MX] ;
struct Edge  // 邻接表
{
    int v ,next ;
}E[MX*2] ;
struct NODE
{
    int u ,v ,w ;
    NODE(){} ;
    NODE(int _u ,int _v ,int _w):u(_u) ,v(_v) ,w(_w){} ;
} ;
vector<NODE>St ;
vector<int>G[MX] ;
void addedge(int u ,int v)
{
    E[num].v = v ; E[num].next = head[u] ; head[u] = num++ ;
    E[num].v = u ; E[num].next = head[v] ; head[v] = num++ ;
}
void dfs_find(int u ,int fa)
{
    dep[u] = dep[fa] + 1 ;
    siz[u] = 1 ;
    son[u] = 0 ;
    father[u] = fa ;
    for(int i = head[u] ; i != -1 ;i = E[i].next)
    {
        int v = E[i].v ;
        if(v == fa)  continue ;
        dfs_find(v ,u) ;
        siz[u] += siz[v] ;
        if(siz[son[u]] < siz[v])  son[u] = v ;
    }
}
void dfs_time(int u ,int fa)
{
    ti[u] = idx++ ;
    top[u] = fa ;
    if(son[u])     dfs_time(son[u] ,top[u]) ;
    for(int i = head[u] ;i != -1 ; i = E[i].next)
    {
        int v = E[i].v ;
        if(v == father[u] || v == son[u])  continue ;
        dfs_time(v ,v) ;
    }
}
void LCA(int u ,int v ,int w)
{
    while(top[u] != top[v])
    {
        if(dep[top[u]] < dep[top[v]])
            swap(u ,v) ;
        St.push_back(NODE(ti[top[u]] ,ti[u]+1 ,w)) ;
        u = father[top[u]] ;
    }
    if(dep[u] > dep[v])
       swap(u ,v) ;
    St.push_back(NODE(ti[u] ,ti[v]+1 ,w)) ;
}
struct node
{
    int le ,rt ,num ,type ;
}T[MX*4] ;
void build(int x ,int le ,int rt) // 建树
{
    T[x].num = 0 ;
    T[x].le = le ; T[x].rt = rt ;
    if(le == rt)
    {
        T[x].type = le ;
        return ;
    }
    int Mid = (le + rt)>>1 ;
    build(L(x) ,le ,Mid) ;
    build(R(x) ,Mid+1 ,rt) ;
}
void push_up(int x)
{
    T[x].num = max(T[L(x)].num ,T[R(x)].num) ;
    if(T[L(x)].num >= T[R(x)].num)
            T[x].type = T[L(x)].type ;
    else    T[x].type = T[R(x)].type ;
}
void update(int x ,int pos ,int add)
{
    if(T[x].le == T[x].rt)
    {
        T[x].num += add ;
        return ;
    }
    int Mid = (T[x].le + T[x].rt)>>1 ;
    if(pos <= Mid)    update(L(x) ,pos ,add) ;
    else    update(R(x) ,pos ,add) ;
    push_up(x) ;
}
void init()
{
    num = 0 ;
    memset(head ,-1 ,sizeof(head)) ;
    memset(ans ,0 ,sizeof(ans)) ;
    memset(P ,0 ,sizeof(P)) ;
    St.clear() ;
    for(int i = 0 ;i <= n+1 ; ++i)
         G[i].clear() ;
}
int main()
{
    //freopen("input.txt" ,"r" ,stdin) ;
    int u ,v ,w ;
    while(scanf("%d%d" ,&n ,&m) ,n+m)
    {
        init() ;
        for(int i = 1 ;i < n ; ++i)
        {
            scanf("%d%d" ,&u ,&v) ;
            addedge(u ,v) ;
        }
        // 树链剖分
        dep[1] = siz[0] = 0 ;
        dfs_find(1 ,1) ;
        idx = 1 ;
        dfs_time(1 ,1) ;
        maxn = 1 ;
        for(int i = 1 ;i <= n ; ++i)   // 转换节点编号
             P[ti[i]] = i ;
        for(int i = 0 ;i < m ; ++i) // 处理询问
        {
            scanf("%d%d%d" ,&u ,&v ,&w) ;
            LCA(u ,v ,w) ;
            maxn = max(w ,maxn) ;
        }
        build(1 ,1 ,maxn) ; //  建树
        int Size = St.size() ;
        for(int i = 0 ;i < Size ; ++i)  // 处理
        {
            int u = St[i].u ,v = St[i].v ,w = St[i].w ;
            G[u].push_back(w) ;
            G[v].push_back(-w) ;
        }
        for(int i = 1 ;i <= n ; ++i) // 利用前缀思想依次遍历每个节点 ,在线段树中的节点
        {
            sort(G[i].begin() ,G[i].end()) ;
            for(int j = 0 ;j < (int)G[i].size() ; ++j) // 插入每个节点
            {
                int color = G[i][j] ;
                if(color < 0)
                          update(1 ,-color ,-1) ;
                else      update(1 ,color ,1) ;
            }
            if(T[1].num)
                  ans[P[i]] = T[1].type ;
            else  ans[P[i]] = 0 ;
        }
        for(int i = 1 ;i <= n ; ++i)
          printf("%d\n" ,ans[i]) ;
    }
    return 0 ;
}
时间: 2024-08-08 13:41:17

HDU 5029 Relief grain(树链剖分)的相关文章

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

题目链接:hdu 5029 Relief grain 题目大意:给定一棵树,然后每次操作在uv路径上为每一个节点加入一个数w,最后输出每一个节点个数最多的那个数. 解题思路:由于是在树的路径上做操作,所以基本就是树链剖分了.仅仅只是曾经是用一个数组就可以维护值,这题要用 一个vector数组记录.过程中用线段树维护最大值. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #i

hdu 5029 Relief grain(树链剖分好题)

Relief grain Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 295    Accepted Submission(s): 66 Problem Description The soil is cracking up because of the drought and the rabbit kingdom is fa

Hdu 5029 Relief grain(熟练剖分)

题目大意: 给出一棵树. 然后有m个操作,每个操作都在两点的路径上分配不同的粮食. 最后要求输出所有村庄有的最多的粮食的种类. 思路分析: 一眼就看得出来是树链剖分的题目. 现在的问题就是,每一次操作,如何维护每个点的最多的食物编号,以及最多的食物的数量.要记录这两个值是肯定的. 首先可以想到将所有的操作按照z排序.这样每一回合操作,称一回合为处理同一种颜色.一回合结束之后,后面的操作就不会影响前面取得的最大值.因为后面的操作的食物的种类不一样,是不可以再继续累加的. 然后可以发现一个规律,就是

HDU Aragorn&#39;s Story -树链剖分

HDU Aragorn's Story Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his k

HDU 5029 Relief grain --熟练剖分第一题

题意:给一棵树,每次给两个节点间的所有节点发放第k种东西,问最后每个节点拿到的最多的东西是哪种. 解法:解决树的路径上的修改查询问题一般用到的是树链剖分+线段树,以前不会写,后来学了一下树链剖分,感觉也不是很难,就是把整个数分成很多链,然后一条重链上的点在线段树中位置是连续的,这样使得更新和查询时更加便利. 这个题目中线段树应该维护的是种类,每次对u-v发放k时,可以让u处+k,v+1处-k,把这些都离线存起来,然后枚举1~n,分别把自己该做的操作都做了,然后统计的时候tree[1].type就

HDU 5029 Relief grain

Relief grain Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 867    Accepted Submission(s): 221 Problem Description The soil is cracking up because of the drought and the rabbit kingdom is fa

HDU 5044 Tree(树链剖分)

HDU 5044 Tree 题目链接 就简单的树链剖分,不过坑要加输入外挂,还要手动扩栈 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 100005; #pragma comment(linker, "/STACK:1024000000,1024000000"

HDU5029--Relief grain (树链剖分+线段树 )

题意:n个点构成的无根树,m次操作, 对于操作 x y z, 表示 x 到 y 路径上的 每个点 加一个 z 数字,可重复加.最后输出每个点 加的次数最多的那个数字,如果没有输出0. 赤裸裸的树链剖分,可是剖分之后 怎么用线段树维护每个点加的数字的次数呢.这里用到的思想类似于2014年上海网络赛的一道题.HDU5044,假如[x,y]这个区间上所有的点加上数字z,可以用两个标记 vec[x] + z,vec[y+1] -c.HDU上C++提交竟然爆栈,不过G++还是顺利ac了.具体见代码 1 #

HDU 5296 Annoying Problem 树链剖分 LCA 倍增法

HDU 5296 Annoying Problem 题目链接:hdu 5296 题意:在一棵给定的具有边权的树,一个节点的集合S(初始为空),给定Q个操作,每个操作增加或删除S中的一个点,每个操作之后输出使集合S中所有点联通的最小子树的边权和. 思路:最小子树上的节点的充要条件: 节点为(S集合中所有点的LCA)的子节点: 节点有一个子孙为S集合中的点. 那么我们给每个节点都开一个标记数组,初始为零,每加入一个节点,就把从这个节点到根节点路径上的点的值都+1,反之-1,这样通过对每个单节点值的查