Submission #1236071

#TimeUsernameProblemLanguageResultExecution timeMemory
1236071htoshiro경주 (Race) (IOI11_race)C++20
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>
#define int long long
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
struct pi {
    int x, y;
};
vector<vector<pi>> adj;
vector<int> sz;
vector<bool> blocked;
vector<int> nxt;
int n, k;
int ans;
int dfs(int cur, int par) {
    sz[cur] = 1;
    for (auto &a : adj[cur]) {
        if (a.x != par && !blocked[a.x]) {
            sz[cur] += dfs(a.x, cur);
        }
    }
    return sz[cur];
}
int cent(int cur, int par, int am) {
    for (auto &a : adj[cur]) {
        if (a.x != par && !blocked[a.x] && sz[a.x] > am / 2) {
            return cent(a.x, cur, am);
        }
    }
    return cur;
}
void dfs2(int cur, int par, int dist, int depth, vector<pair<int, int>> &paths) {
    if (dist > k) return;
    paths.emplace_back(dist, depth);
    for (auto &a : adj[cur]) {
        if (a.x != par && !blocked[a.x]) {
            dfs2(a.x, cur, dist + a.y, depth + 1, paths);
        }
    }
}
int build(int cur, int par) {
    int total = dfs(cur, -1);
    int curc = cent(cur, -1, total);
    blocked[curc] = true;
    nxt[curc] = par;
    unordered_map<int, int> dist_count;
    dist_count[0] = 0; 
    int best = LLONG_MAX;
    for (auto &a : adj[curc]) {
        if (!blocked[a.x]) {
            vector<pair<int, int>> paths;
            dfs2(a.x, curc, a.y, 1, paths);
            for (auto &[d, l] : paths) {
                if (dist_count.count(k - d)) {
                    best = min(best, l + dist_count[k - d]);
                }
            }
            for (auto &[d, l] : paths) {
                if (!dist_count.count(d)) dist_count[d] = l;
                else dist_count[d] = min(dist_count[d], l);
            }
        }
    }
    for (auto &a : adj[curc]) {
        if (!blocked[a.x]) {
            best = min(best, build(a.x, curc));
        }
    }
    return best;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> k;
    adj.resize(n);
    sz.resize(n);
    nxt.resize(n);
    blocked.assign(n, false);
    for (int i = 0; i < n - 1; i++) {
        int x, y, h;
        cin >> x >> y >> h;
        adj[x].push_back({y, h});
        adj[y].push_back({x, h});
    }
    ans = build(0, -1);
    cout << (ans == LLONG_MAX ? -1 : ans) << '\n';
    return 0;
}

Compilation message (stderr)

/usr/bin/ld: /tmp/ccjGX6ym.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccEMg2n5.o:race.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccjGX6ym.o: in function `main':
grader.cpp:(.text.startup+0x28): undefined reference to `best_path(int, int, int (*) [2], int*)'
collect2: error: ld returned 1 exit status