P1084 二分 + 树上倍增 + 贪心

题意

传送门 P1084 疫情控制

题解

t t t 时间内可以完成控制疫情,那么 t ′ ≥ t t^{'}\geq t tt 时间内仍然可以控制疫情,那么二分答案。问题转换为在已知军队可移动的最长时间情况下,能否控制疫情。

在不移动到根节点的情况下,军队向上移动到的节点深度越浅,能管辖的叶子节点越多,通过树上倍增预处理,则可以 O ( log ⁡ n ) O(\log n) O(logn) 求解节点向上移动的位置。对于能够到达根节点的军队,有可能跨过根节点去管辖其他子树,最优的做法是去管辖根节点的子节点,设其为 f c h fch fch,将这类军队移动到所在子树的根节点 f c h i fch_i fchi;其余节点的位置显然是确定的。

对能够确定的节点位置做标记, D F S DFS DFS 求出以 f c h fch fch 为根节点的子树是否已被管辖,那么对于已经被管辖的 f c h i fch_i fchi,就可以将此节点上未确定位置的军队用以管辖其余 f c h j , i ≠ j fch_j,i\neq j fchj,i=j

对于未确定位置的军队,以及未被管辖的 f c h i fch_i fchi,可以贪心地将当前距离根节点最近的 f c h i fch_i fchi 交给可以移动到它且剩余移动时间最短的军队。此时需要考虑跨过根节点后无法返回原本所在 f c h i fch_i fchi 的军队,若 f c h i fch_i fchi 未被管辖,那么将其驻扎在 f c h i fch_i fchi 显然是一个可能的策略,将满足此类条件的剩余时间最小的军队进行驻扎;若由其余军队驻扎,显然交换后可以得到剩余可移动时间更多的军队。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 50005, maxlg = 16, inf = 0x3f3f3f3f;
int N, M, rt = 1, lg[maxn], fa[maxlg][maxn], dep[maxn], id[maxn];
ll sum, tim[maxlg][maxn];
int head[maxn], nxt[maxn << 1], to[maxn << 1], cost[maxn << 1], tot;
vector<int> fch;
vector<ll> _fch[maxn], ch, army;
bool used[maxn];

void add(int x, int y, int z) { to[++tot] = y, cost[tot] = z, nxt[tot] = head[x], head[x] = tot; }

void dfs(int x, int f, int d, int w)
{
    fa[0][x] = f, dep[x] = d, tim[0][x] = w;
    for (int k = 1; k <= lg[d]; ++k)
    {
        tim[k][x] = tim[k - 1][x] + tim[k - 1][fa[k - 1][x]];
        fa[k][x] = fa[k - 1][fa[k - 1][x]];
    }
    for (int i = head[x]; i; i = nxt[i])
    {
        int y = to[i], z = cost[i];
        if (y != f)
            dfs(y, x, d + 1, z);
    }
}

bool cdfs(int x, int f)
{
    if (used[x])
        return 1;
    bool c = 0, res = 1;
    for (int i = head[x]; i; i = nxt[i])
        if (to[i] != f)
            c = 1, res &= cdfs(to[i], x);
    return used[x] = c ? res : used[x];
}

bool judge(ll m)
{
    memset(used, 0, sizeof(used));
    for (auto &c : fch)
        _fch[c].clear();
    ch.clear(), army.clear();
    for (int i = 1; i <= M; ++i)
    {
        int x = id[i];
        ll t = 0;
        for (int k = lg[dep[x]]; k >= 0;)
            if (t + tim[k][x] <= m && fa[k][x] != rt)
                t += tim[k][x], x = fa[k][x], k = lg[dep[x]];
            else
                --k;
        ll rst = m - t - tim[0][x];
        if (rst >= 0)
            _fch[x].push_back(rst);
        else
            used[x] = 1;
    }
    for (auto &c : fch)
    {
        cdfs(c, rt);
        if (used[c])
            for (auto &t : _fch[c])
                army.push_back(t);
        else
        {
            if (_fch[c].size() == 0)
                ch.push_back(tim[0][c]);
            else
            {
                sort(_fch[c].begin(), _fch[c].end());
                if (_fch[c][0] >= tim[0][c])
                    army.push_back(_fch[c][0]), ch.push_back(tim[0][c]);
                for (int i = 1; i < (int)_fch[c].size(); ++i)
                    army.push_back(_fch[c][i]);
            }
        }
    }
    sort(ch.begin(), ch.end());
    sort(army.begin(), army.end());
    int a = 0;
    for (auto &t : ch)
    {
        while (a < (int)army.size() && t > army[a])
            ++a;
        if (a == (int)army.size())
            return 0;
        ++a;
    }
    return 1;
}

int main()
{
    scanf("%d", &N);
    for (int i = 1; i < N; ++i)
    {
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        sum += z;
        add(x, y, z), add(y, x, z);
    }
    scanf("%d", &M);
    for (int i = 1; i <= M; ++i)
        scanf("%d", id + i);
    lg[0] = -1;
    for (int i = 1; i < N; ++i)
        lg[i] = lg[i - 1] + (1 << (lg[i - 1] + 1) == i);
    for (int i = head[rt]; i; i = nxt[i])
        fch.push_back(to[i]);
    dfs(rt, 0, 0, 0);
    ll lb = -1, ub = sum + 1;
    while (ub - lb > 1)
    {
        ll mid = (ub + lb) >> 1;
        if (judge(mid))
            ub = mid;
        else
            lb = mid;
    }
    printf("%lld\n", ub > sum ? -1LL : ub);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值