제출 #571194

#제출 시각아이디문제언어결과실행 시간메모리
571194stevancvRace (IOI11_race)C++14
100 / 100
947 ms52408 KiB
#include <bits/stdc++.h>
#include "race.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()) smin(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 v : tmp) {
            if (m.find(v.first) == m.end()) m[v.first] = v.second;
            else smin(m[v.first], v.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;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...