답안 #1084048

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1084048 2024-09-04T23:51:29 Z sonho00 경주 (Race) (IOI11_race) C++17
0 / 100
1 ms 2392 KB
#include <bits/stdc++.h>
using namespace std;

struct CD {
    int n;
    vector<vector<int>> adj, tree;
    vector<int> sz;
    vector<bool> used;
    CD(int n) : n(n), adj(n + 1), tree(n + 1), sz(n + 1), used(n + 1) {}
    int getSz(int cur, int par) {
        sz[cur] = 1;
        for (int nei : adj[cur]) {
            if (used[nei] || nei == par) continue;
            sz[cur] += getSz(nei, cur);
        }
        return sz[cur];
    }
    int getCent(int cur, int tot, int par) {
        for (int nei : adj[cur]) {
            if (used[nei] || nei == par) continue;
            if (tot < sz[nei] * 2) return getCent(nei, tot, cur);
        }
        return cur;
    }
    int getRoot(int cur = 1) {
        cur = getCent(cur, getSz(cur, -1), -1);
        used[cur] = 1;
        for (int nei : adj[cur]) {
            if (used[nei]) continue;
            tree[cur].push_back(getRoot(nei));
        }
        return cur;
    }
};

signed best_path(int n,int k,int h[200000][2],int l[200000]) {
    CD cd(n);
    vector<vector<pair<int, int>>> adj(n);
    for (int i = 0; i < n-1; ++i) {
        cd.adj[h[i][0]].push_back(h[i][1]);
        cd.adj[h[i][1]].push_back(h[i][0]);
        adj[h[i][0]].emplace_back(l[i], h[i][1]);
        adj[h[i][1]].emplace_back(l[i], h[i][0]);
    }
    vector<bool> used(n);
    vector<int> dp(k + 1, n);
    int ans = n;
    function<void(int, int, int, int, int)> dfs =
        [&](int cur, int par, int dist, int dep, int type) {
            if (dist > k) return;
            if (type == 1) ans = min(ans, dep + dp[k - dist]);
            if (type == 2) dp[dist] = min(dp[dist], dep);
            if (type == 3) dp[dist] = n;
            for (auto &[wei, nei] : adj[cur]) {
                if (used[nei] || nei == par) continue;
                dfs(nei, cur, dist + wei, dep + 1, type);
            }
        };
    function<void(int)> solve = [&](int root) {
        used[root] = 1;
        dp[0] = 0;
        for (auto &[wei, nei] : adj[root]) {
            if (used[nei]) continue;
            dfs(root, -1, 0, 0, 1);
            dfs(root, -1, 0, 0, 2);
        }
        for (auto &[wei, nei] : adj[root]) {
            if (used[nei]) continue;
            dfs(root, -1, 0, 0, 3);
        }
        for (int nei : cd.tree[root]) {
            if (used[nei]) continue;
            solve(nei);
        }
    };
    solve(cd.getRoot(0));
    if (ans == n) ans = -1;
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2392 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2392 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2392 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2392 KB Output isn't correct
2 Halted 0 ms 0 KB -