제출 #571161

#제출 시각아이디문제언어결과실행 시간메모리
571161stevancv경주 (Race) (IOI11_race)C++14
컴파일 에러
0 ms0 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 5e5 + 2;
const int M = 1e9;
int mod = 1000000007;
vector<array<int, 2>> g[N];
bool was[N];
int sz[N], k;
void Dfs(int s, int e) {
    sz[s] = 1;
    for (auto u : g[s]) {
        if (u[0] == e || was[u[0]]) continue;
        Dfs(u[0], s);
        sz[s] += sz[u[0]];
    }
}
int Find(int s, int e, int n) {
    for (auto u : g[s]) {
        if (u[0] == e || was[u[0]]) continue;
        if (2 * sz[u[0]] > n) return Find(u[0], s, n);
    }
    return s;
}
int FindCentroid(int s) {
    Dfs(s, -1);
    return Find(s, -1, sz[s]);
}
map<ll, int> m;
int ans;
vector<pair<ll, int>> tmp;
void Solve(int s, int e, int d, ll o) {
    if (o > k || d > ans) return;
    tmp.push_back({o, d});
    if (m.find(k - o) != m.end()) smax(ans, d + m[k - o]);
    for (auto u : g[s]) {
        if (u[0] == e || was[u[0]]) continue;
        Solve(u[0], s, d + 1, o + u[1]);
    }
}
void Decompose(int s) {
    s = FindCentroid(s);
    was[s] = 1;
    m.clear();
    m[0] = 0;
    for (auto u : g[s]) {
        if (was[u[0]]) continue;
        tmp.clear();
        Solve(u[0], s, 1, u[1]);
        for (auto u : tmp) {
            if (m.find(u.first) == m.end()) m[u.first] = u.second;
            else smin(m[u.first], u.second);
        }
    }
    for (auto u : g[s]) {
        if (was[u[0]] == 0) Decompose(u[0]);
    }
}
int best_path(int n, int K, int h[][2], int l[]) {
    k = K;
    for (int i = 0; i < n - 1; i++) {
        g[h[i][0]].push_back({h[i][1], l[i]});
        g[h[i][1]].push_back({h[i][0], l[i]});
    }
    ans = n;
    Decompose(0);
    if (ans == n) ans = -1;
    return ans;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, k;
    cin >> n >> k;
    int h[n - 1][2], l[n - 1];
    for (int i = 0; i < n - 1; i++) {
        cin >> h[i][0] >> h[i][1] >> l[i];
    }
    cout << best_path(n, k, h, l) << en;
    return 0;
}
/*
11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
*/

컴파일 시 표준 에러 (stderr) 메시지

/usr/bin/ld: /tmp/ccm0g1P3.o: in function `main':
race.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccOQP5Y1.o:grader.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status