---
id: 199
lang: en
translation_of: index.md
source_sha256: 9935c29ceb0d3e98a5b7118569ec2a5c3b64fd22af219c65970ae645f8fc9395
title: "POJ2054: Color a Tree — Solution"
description: "Greedily color the subtree with the largest average cost next, merging each chosen node into its parent, with an O(n) search for the maximum."
tags: ["OI","Trees","Greedy","POJ","Greedy on Trees"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "算法进阶教程 rendered literally as “the advanced algorithms tutorial”, as the author wrote it, rather than by the book's full title."
---

## Approach

Lately I've been working through problems along with the advanced algorithms tutorial written by Li Yudong. This problem has the following constraints:

- A child node can only be colored once its parent node has been colored.
- We want the minimum coloring cost.

Thinking about these two conditions, we can conclude that as long as we give priority to coloring the subtree with the largest average cost, we get the optimal solution; also, clearly, if we color a parent node, then the child with the largest average cost among its children will be colored first.

So that more people can understand it, I'm going to explain this solution by splitting the code into pieces.

<!--more-->

## Code Blocks

```cpp
struct node
{
    int tim, cost, fat;
    double w;
} nums[maxn];
```

We define a data structure, with fields for the count, the cost, the parent node and the average weight.

```cpp
int find_max()
{
    double val = 0;
    int id = 1;
    for (int i = 1; i <= n; i++)
        if (i != root && nums[i].w > val)
            id = i, val = nums[i].w;
    return id;
}
```

To find the node with the largest average weight, we search in an $O(n)$ way. I'm sure there are better approaches; forgive me for only being able to write down one of them here. Note that during the search we need to exclude the $root$ node.

```cpp
int main()
{
    while (scanf("%d%d", &n, &root) && n != 0)
    {
        long long ans = 0;
        for (int i = 1; i <= n; i++)
            scanf("%d", &nums[i].cost), nums[i].w = nums[i].cost, nums[i].tim = 1, ans += nums[i].cost;
        for (int i = 1; i < n; i++)
            scanf("%d%d", &x, &y), nums[y].fat = x;
        for (int i = 1; i < n; i++)
        {
            int idx = find_max();
            nums[idx].w = 0;
            ans += nums[idx].cost * nums[nums[idx].fat].tim;
            for (int j = 1; j <= n; j++)
                if (nums[j].fat == idx)
                    nums[j].fat = nums[idx].fat;
            nums[nums[idx].fat].cost += nums[idx].cost;
            nums[nums[idx].fat].tim += nums[idx].tim;
            nums[nums[idx].fat].w = double(nums[nums[idx].fat].cost) / nums[nums[idx].fat].tim;
        }
        printf("%lld\n", ans);
    }
    return 0;
}
```

Next comes the main function. Let's look at the code inside the loop first. We first read in and initialize the $nums[]$ array, initializing the cost and the count, and add each node's cost to the answer (this has to be added). Then we read the edges. After that, we start computing. Our range is $[1,n)$, because we have already added every node's base value to the answer, and there is always one node whose cost only needs to be multiplied by one, so we can set the range to this interval.

We find the node with the largest average cost and set its cost to $0$, so that later searches will exclude it. Then we merge all of its children onto the parent of node $idx$. After that, we merge some of the two nodes' attributes.

Finally we output it and get the answer. Full code:

```cpp
// POJ2054.cpp
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 1010;
int n, root, x, y;
struct node
{
    int tim, cost, fat;
    double w;
} nums[maxn];
int find_max()
{
    double val = 0;
    int id = 1;
    for (int i = 1; i <= n; i++)
        if (i != root && nums[i].w > val)
            id = i, val = nums[i].w;
    return id;
}
int main()
{
    while (scanf("%d%d", &n, &root) && n != 0)
    {
        long long ans = 0;
        for (int i = 1; i <= n; i++)
            scanf("%d", &nums[i].cost), nums[i].w = nums[i].cost, nums[i].tim = 1, ans += nums[i].cost;
        for (int i = 1; i < n; i++)
            scanf("%d%d", &x, &y), nums[y].fat = x;
        for (int i = 1; i < n; i++)
        {
            int idx = find_max();
            nums[idx].w = 0;
            ans += nums[idx].cost * nums[nums[idx].fat].tim;
            for (int j = 1; j <= n; j++)
                if (nums[j].fat == idx)
                    nums[j].fat = nums[idx].fat;
            nums[nums[idx].fat].cost += nums[idx].cost;
            nums[nums[idx].fat].tim += nums[idx].tim;
            nums[nums[idx].fat].w = double(nums[nums[idx].fat].cost) / nums[nums[idx].fat].tim;
        }
        printf("%lld\n", ans);
    }
    return 0;
}
```
