---
id: 1752
lang: en
translation_of: index.md
source_sha256: 3654bffdadb20abe8bda2d82f487c3300071fee76419810ede62ad829372c87f
title: "[NOIp 2012 Epidemic Control] Solution"
description: "Binary search on the time, make every node jump upward, then allocate across subtrees, pairing coverage needs greedily with a heap."
tags: ["OI","Graph Theory","Binary Search on the Answer"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "点 (points/nodes) rendered as “nodes”; the author means the armies' positions on the tree."
---

## Main Idea

You can probably come up with binary searching on the time, then making every node jump upward, and then allocating the nodes that can be assigned across subtrees. That's pretty much the idea, and it's quite a natural one.

But there are some details worth noting: the priority between cross-subtree allocation and covering the current subtree has to be handled properly. If you don't handle this right, you'll only pass 80% of the data on Luogu. I spent a long time fixing this part, and in the end found that you need to make cross-subtree allocation the first enumeration, and then, in between, do a second enumeration over the subtrees' coverage needs (put them in a heap and pair them greedily).

<!--more-->

## Code

```cpp
// P1084.cpp
#include <bits/stdc++.h>
#define ll long long
#define pr pair<ll, int>

using namespace std;

const int MAX_N = 1e5 + 200;

int head[MAX_N], n, m, current, siz[MAX_N], dep[MAX_N];
int fa[25][MAX_N], idx[MAX_N], deg[MAX_N], org[MAX_N];
ll dist[MAX_N];
bool tag[MAX_N], verification[MAX_N];
priority_queue<pr> pq, root_q;

struct edge
{
    int to, nxt, weight;
} edges[MAX_N << 1];

void addpath(int src, int dst, int weight)
{
    edges[current].to = dst, edges[current].nxt = head[src];
    edges[current].weight = weight, head[src] = current++;
}

void dfs(int u, int fat, int og)
{
    siz[u] = 1, dep[u] = dep[fat] + 1, org[u] = og, fa[0][u] = fat;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fat)
            dist[edges[i].to] = dist[u] + edges[i].weight, dfs(edges[i].to, u, dep[u] == 1 ? edges[i].to : og), siz[u] += siz[edges[i].to];
}

void mark(int u, int fa, bool covered)
{
    if (siz[u] == 1 && covered == false)
        verification[org[u]] = false;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa)
            mark(edges[i].to, u, covered || tag[u]);
}

bool check(ll mid)
{
    // storing the index;
    // initialize;
    memset(verification, true, sizeof(verification));
    memset(tag, false, sizeof(tag));
    while (!pq.empty())
        pq.pop();
    while (!root_q.empty())
        root_q.pop();
    // find the redundent stuff;
    for (int i = 1; i <= idx[0]; i++)
        if (dist[idx[i]] <= mid)
            root_q.push(make_pair(-(mid - dist[idx[i]]), i));
        else
        {
            ll tmp_dist = mid;
            int u = idx[i];
            for (int j = 24; j >= 0; j--)
                if (fa[j][u] != 0 && dist[u] - dist[fa[j][u]] <= tmp_dist)
                    tmp_dist -= dist[u] - dist[fa[j][u]], u = fa[j][u];
            tag[u] = true;
        }
    mark(1, 0, false);
    for (int i = head[1]; i != -1; i = edges[i].nxt)
        if (verification[edges[i].to] == false)
            pq.push(make_pair(-dist[edges[i].to], edges[i].to));
    // fulfill their needs;
    while (!root_q.empty() && !pq.empty())
    {
        pr curt = root_q.top();
        root_q.pop();
        if ((-curt.first) >= -pq.top().first)
            verification[pq.top().second] = true;
        else
            verification[org[idx[curt.second]]] = true;
        while (!pq.empty() && verification[pq.top().second] == true)
            pq.pop();
    }
    return pq.empty();
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d", &n);
    for (int i = 1, u, v, w; i <= n - 1; i++)
        scanf("%d%d%d", &u, &v, &w), addpath(u, v, w), addpath(v, u, w), deg[u]++, deg[v]++;
    scanf("%d", &idx[0]);
    for (int i = 1; i <= idx[0]; i++)
        scanf("%d", &idx[i]);
    if (deg[1] > idx[0])
        puts("-1"), exit(0);
    dfs(1, 0, 0);
    for (int i = 1; i < 25; i++)
        for (int j = 1; j <= n; j++)
            fa[i][j] = fa[i - 1][fa[i - 1][j]];
    ll l = 0, r = 1e18, ans = 0;
    while (l <= r)
    {
        ll mid = (l + r) >> 1;
        if (check(mid))
            r = mid - 1, ans = mid;
        else
            l = mid + 1;
    }
    printf("%lld", ans);
    return 0;
}
```
