H - Relief grain


题目描述:

H - Relief grain
Time Limit:5000MS Memory Limit:100000KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

HDU 5029
Appoint description:
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}.

题解:

和队友在比赛的时候想出来了还是略开心啊(⊙o⊙).
发现总共修改了1e5次,如果每一次我们只用很少的点来描述这次修改,那么就不会爆内存.于是想到就像差分一样打两个点,一个add Z 一个 sub Z,但是树上的链不是线性的,于是用树链剖分,把一个链变成了<=2*log个线性的链,然后在链上类似于差分打点. 最后算结果的时候一起算一次,用一个线段树来维护结果.

重点:

观察到可以用很少的点来描述修改,因为整道题目只算了一次答案,所以我们可以差分着打点.
然后发现如果用dfs序的树链剖分就比较好写,因为变成线性的啦.

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>

using namespace std;

const int maxn = 1e5 + 100;
const int MAX_NODE = 4*maxn + 100;
const int MAX_T = 5e6+100;
const int maxZ = 100000;

int fa[maxn], son[maxn], num[maxn], top[maxn], deep[maxn], getno[maxn], fanno[maxn], tot;
vector<int> G[maxn];
//vector<int> add[maxn], sub[maxn];
struct vec
{
    int head[maxn], nxt[MAX_T], id[MAX_T];
    int tot;
    void addEdge(int a, int z)
    {
        nxt[tot] = head[a];
        id[tot] = z;
        head[a] = tot;
        tot++;
    }
};
vec add, sub;
int ans[maxn];
int n, M;
struct info
{
    int num, id;
    info(int _num = 0, int _id = 0)
    {
        num = _num;
        id = _id;
    }
};
info tree[MAX_NODE];
info getMerge(info a, info b)
{
    if(a.num > b.num)
        return a;
    if(b.num > a.num)
        return b;
    if(b.id < a.id)
        return b;
    return a;
}
void pushUp(int rt)
{
    int lrt = (rt<<1), rrt = lrt+1;
    if(tree[rrt].num > tree[lrt].num)
    {
        tree[rt] = tree[rrt];
    }
    else
    {
        tree[rt] = tree[lrt];
    }
}
void initail(int rt, int l, int r)
{
    if(l==r)
    {
        tree[rt].num = 0;
        tree[rt].id = l;
        return;
    }
    int lrt = (rt<<1), rrt = lrt+1, m = (l+r)/2;
    initail(lrt, l, m);
    initail(rrt, m+1, r);
    pushUp(rt);
}
void change(int pos, int val, int rt, int l, int r)
{
    if(pos==l&&pos==r)
    {
        tree[rt].num += val;
        return;
    }
    int lrt = (rt<<1), rrt = lrt+1, m = (l+r)/2;
    if(pos <= m)
    {
        change(pos, val, lrt, l, m);
    }
    else
    {
        change(pos, val, rrt, m+1, r);
    }
    pushUp(rt);
}
info query(int L, int R, int rt, int l, int r)
{
    if(L <= l && R >= r)
    {
        return tree[rt];
    }
    info t;
    t.num = -1;
    int lrt = (rt<<1), rrt = lrt+1, m = (l+r)/2;
    if(L <= m)
    {
        info tmp = query(L, R, lrt, l, m);
        t = getMerge(t, tmp);
    }
    if(R >= m+1)
    {
        info tmp = query(L, R, rrt, m+1, r);
        t = getMerge(t, tmp);
    }
    return t;
}

void dfs_first(int u, int pa)
{
    num[u] = 1;
    son[u] = 0;
    for(int i = 0;i<G[u].size();i++)
    {
        int v = G[u][i];
        if(v!=pa)
        {
            fa[v] = u;
            deep[v] = deep[u]+1;
            dfs_first(v, u);
            if(num[v] > num[son[u]])
            {
                son[u] = v;
            }
            num[u] += num[v];
        }
    }
}
void dfs_second(int u, int tp)
{
    tot++;
    getno[u] = tot;
    fanno[tot] = u;
    top[u] = tp;
    if(son[u] != 0)
    {
        dfs_second(son[u], tp);
    }
    for(int i = 0;i<G[u].size();i++)
    {
        int v = G[u][i];
        if(v!=fa[u] && v!=son[u])
        {
            dfs_second(v, v);
        }
    }
}
void gao(int l, int r, int z)
{
    add.addEdge(l, z);
    //add[l].push_back(z);
//    if(r < n)
//        sub[r+1].push_back(z);
    if(r < n)
        sub.addEdge(r+1, z);
}
void change(int a, int b, int z)
{
    int tpa = top[a], tpb = top[b];
    while(tpa!=tpb)
    {
        if(deep[tpa] < deep[tpb])
        {
            swap(tpa, tpb);
            swap(a, b);
        }
        gao(getno[tpa], getno[a], z);
        a = fa[tpa];
        tpa = top[a];
    }
    if(deep[a] < deep[b])
        swap(a, b);
    gao(getno[b], getno[a], z);
}

void solve()
{
    num[0] = 0;
    fa[1] = 0;
    deep[1] = 0;
    dfs_first(1, 0);
    tot = 0;
    dfs_second(1, 1);
    for(int i = 1;i<=n;i++)
    {
        add.head[i] = -1;
        sub.head[i] = -1;
    }
    add.tot = 0;
    sub.tot = 0;
    //printf("*******%d\n", tot);
    for(int i = 1;i<=M;i++)
    {
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        change(x, y, z);
    }
    initail(1, 1, maxZ);
    for(int i = 1;i<=n;i++)
    {
        for(int j = add.head[i];j!=-1;j = add.nxt[j])
        {
            int num = add.id[j];
            change(num, 1, 1, 1, maxZ);
        }
        for(int j = sub.head[i];j!=-1;j = sub.nxt[j])
        {
            int num = sub.id[j];
            change(num, -1, 1, 1, maxZ);
        }
        info t = query(1, maxZ, 1, 1, maxZ);
        if(t.num==0)
        {
            ans[fanno[i]] = 0;
        }
        else
        {
            ans[fanno[i]] = t.id;
        }
    }
    for(int i = 1;i<=n;i++)
    {
        printf("%d\n", ans[i]);
    }
}

int main()
{
    //freopen("Hin.txt", "r", stdin);
    while(scanf("%d%d", &n, &M) != EOF)
    {
        if(n==0&&M==0)
            break;
        for(int i = 1;i<=n;i++)
            G[i].clear();
        for(int i = 1;i<=n-1;i++)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            G[a].push_back(b);
            G[b].push_back(a);
        }
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值