codeforce 600E

codeforces 600E E. Lomsat gelral

Portal: https://codeforces.com/contest/600/problem/E

Meaning of the questions:

Give you a tree of n nodes, each node of the tree has a color, ask the largest number of those color values ​​of each color sub-tree node where the sum

answer:

Consider the practice of violence, it can be expressed by an array of all states. Then consider merging state, then open one such array for each node, obviously not enough space. Then opened dynamic point, for combined, arrays into o (n), consider the line segment tree merge.

 

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 10;
const int M = 2e5 + 10;

int n;
int h [N] and [M], ne [M], idx;

struct node
{
    ll dat, cnt;
    you have to, to;
} p [N * 30 ];

int root [N], to;
ll w[N], ans[N];

void add(int a, int b)
{
    e[idx] = b;
    ne [idx] = h [a];
    h[a] = idx ++;
}

void update(int p)
{
    if(tr[tr[p].l].cnt > tr[tr[p].r].cnt)
    {
        p [p] .cnt = p [p [p] .l] .cnt;
        tr [p] .dat = tr [tr [n] .L] .dat;
        return ;
    }
    else if(tr[tr[p].l].cnt < tr[tr[p].r].cnt)
    {
        p [p] .cnt = p [p [p] .r] .cnt;
        tr [p] .dat = tr [tr [p] .r] .dat;
        return ;
    }
    else
    {
        p [p] .cnt = p [p [p] .l] .cnt;
        tr [p] .dat = tr [tr [n] .L] .dat + tr [tr [p] .r] .dat;
        return ;
    }
}

void insert(int &p, int l, int r, int c)
{
    if(!p) p = ++ tot;
    if(l == r)
    {
        tr[p].cnt ++;
        tr[p].dat = c;
        return ;
    }
    int mid = l + r >> 1;
    if(c <= mid) insert(tr[p].l, l, mid, c);
    else insert(tr[p].r, mid + 1, r, c);
    update(p);
}

int merge ( int n, int g, int i, int i)
{
    if(!p) return q;
    if(!q) return p;
    if(l == r)
    {
        tr[p].cnt += tr[q].cnt;
        return p;
    }
    int mid = l + r >> 1;
    tr[p].l = merge(tr[p].l, tr[q].l, l, mid);
    tr[p].r = merge(tr[p].r, tr[q].r, mid + 1, r);
    update(p);
    return p;
}

void dfs(int x, int fa)
{
    insert(root[x], 1, n, w[x]);
    for (int i = h[x]; ~i; i = ne[i])
    {
        int j = e[i];
        if(j == fa) continue;
        dfs(j, x);
        root[x] = merge(root[x], root[j], 1, n);
    }
    ans [x] = row [root [x]] that is.;
}

int main ()
{
    memset(h, -1, sizeof(h));
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++)
        scanf("%lld", &w[i]);
    for (int i = 0; i < n - 1; i ++)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        add(a, b), add(b, a);
    }
    dfs(1, -1);
    for (int i = 1; i <= n; i ++)
    {
        printf("%lld%c", ans[i], i == n? '\n' : ' ');
    }
}

Guess you like

Origin www.cnblogs.com/xwdzuishuai/p/12061803.html