使用lambda 就RE了,不用就A了
查看原帖
使用lambda 就RE了,不用就A了
21031
NaCN楼主2020/12/8 23:15

这是RE代码

#include <bits/stdc++.h>
using namespace std;
#define ll long long

int read()
{
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

const int N = 3e5 + 10;

vector<pair<int, int>> g[N];
namespace Tree_Po
{
    int top[N], son[N], dep[N], f[N], siz[N], dfn[N];

    int tot;

    void dfs1(int x, int fa)
    {
        dep[x] = dep[fa] + 1;
        siz[x] = 1;
        f[x] = fa;
        for (auto [to, w] : g[x])
        {
            if (to == fa)
                continue;
            dfs1(to, x);
            siz[x] += siz[to];
            if (siz[to] > siz[son[x]])
            {
                son[x] = to;
            }
        }
    }
    void dfs2(int x, int fa, int tp)
    {
        top[x] = tp;
        dfn[x] = ++tot;
        if (son[x] != 0)
        {
            dfs2(son[x], x, tp);
        }
        for (auto [to, w] : g[x])
        {
            if (to == fa || to == son[x])
                continue;
            dfs2(to, x, to);
        }
    }
    int query_lca(int u, int v)
    {
        while (top[u] != top[v])
        {
            if (dep[top[u]] < dep[top[v]])
                swap(u, v);
            u = f[top[u]];
        }
        if (dep[u] > dep[v])
            swap(u, v);
        return u;
    }

    void init()
    {
        dfs1(1, 0);
        dfs2(1, 0, 1);
    }
} // namespace Tree_Po

int main()
{
    int n = read(), m = read();
    for (int i = 1; i < n; i++)
    {
        int u = read(), v = read(), w = read();
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    vector<pair<int, int>> tmp(m);
    vector<int> dis(n + 1);
    for (int i = 0; i < m; i++)
        tmp[i].first = read(), tmp[i].second = read();

    function<void(int, int)> dfs1 = [&](int x, int fa) {
        for (auto [to, w] : g[x])
        {
            if (to == fa)
                continue;
            dis[to] = dis[x] + w;
            dfs1(to, x);
        }
    };
    auto check = [&](int k) {
        int cnt = 0;
        int mx = 0;
        vector<int> d(n);
        for (auto [u, v] : tmp)
        {
            int o = Tree_Po::query_lca(u, v);

            if (dis[u] + dis[v] - 2 * dis[o] > k)
            {
                cnt++;
                d[u]++;
                d[v]++;
                d[o] -= 2;
                //  d[Tree_Po::f[o]]--;
                mx = max(dis[u] + dis[v] - 2 * dis[o], mx);
            }
        }

        if (!cnt)
            return 1;
        int res = 0;
        function<void(int, int, int)> dfs2 = [&](int x, int fa, int w) {
            for (auto [to, w] : g[x])
            {
                if (to == fa)
                    continue;
                dfs2(to, x, w);
                d[x] += d[to];
            }
            if (d[x] == cnt)
            {
                // cout << x << endl;
                res = max(res, w);
            }
        };
        dfs2(1, 0, 0);
        if (mx - res <= k)
            return 1;
        else
            return 0;
    };
    Tree_Po::init();
    dfs1(1, 0);

    int l = 0, r = 1e9, ans;
    while (l <= r)
    {
        int mid = (l + r) >> 1;
        if (check(mid))
        {
            ans = mid;
            r = mid - 1;
        }
        else
            l = mid + 1;
    }
    printf("%d\n", ans);
}

求助?

2020/12/8 23:15
加载中...