This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
ID: varunra2
LANG: C++
TASK: race
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif
#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second
const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions
int n, k;
vector<VII> adj;
VVI centadj;
VI cent;
VI ret;
struct centroid {
vector<bool> vis;
VI sub;
void init() {
vis.assign(n, false);
sub.resize(n);
cent.resize(n);
centadj.resize(n);
adj.resize(n);
ret.assign(n, 2 * INF);
}
int dfs(int u, int v) {
sub[u] = 1;
for (auto& x : adj[u]) {
if (x.x != v and vis[x.x] == false) {
sub[u] += dfs(x.x, u);
}
}
return sub[u];
}
int getCent(int u, int siz, int v) {
for (auto& x : adj[u]) {
if (x.x != v and vis[x.x] == false) {
if (sub[x.x] * 2 > siz) {
return getCent(x.x, siz, u);
}
}
}
return u;
}
void centr(int u, int lev, int par = -1) {
int c = getCent(u, dfs(u, -1), -1);
cent[c] = ++lev;
vis[c] = true;
if (par != -1) {
centadj[par].PB(c);
}
for (auto& x : adj[c]) {
if (vis[x.x] == false) {
centr(x.x, lev, c);
}
}
}
} centroid1;
void dfs(int u, int v, int og, int dist1, int dist2, VII& cur) {
if (cent[u] <= cent[og]) return;
if (dist1 > 10 * k) return; // you can do > k, but just to be safe lmao
// also im doing this so i dont have to use long long
cur.PB(MP(dist1, dist2));
for (auto& x : adj[u]) {
if (x.x == v) continue;
dfs(x.x, u, og, dist1 + x.y, dist2 + 1, cur);
}
}
void solve(int u) {
// first you solve for all of your children
trav(x, centadj[u]) { solve(x); }
MPII mp;
VII cur;
mp[0] = 0;
for (auto& x : adj[u]) {
cur.clear();
dfs(x.x, u, u, x.y, 1, cur);
if (cur.empty()) continue;
trav(y, cur) {
if (mp[k - y.x] > 0) ret[u] = min(ret[u], mp[k - y.x] + y.y);
if (y.x == k) {
ret[u] = min(ret[u], y.y);
}
}
trav(y, cur) {
if (mp[y.x] == 0)
mp[y.x] = y.y;
else
mp[y.x] = min(mp[y.x], y.y);
}
}
}
int best_path(int N, int K, int h[][2], int l[]) {
n = N;
k = K;
centroid1.init();
for (int i = 0; i < n - 1; i++) {
int u, v, w;
u = h[i][0], v = h[i][1], w = l[i];
adj[u].PB(MP(v, w));
adj[v].PB(MP(u, w));
}
centroid1.centr(0, 0);
int root = min_element(all(cent)) - cent.begin();
solve(root);
int pr = *min_element(all(ret));
if (pr == 2 * INF) return -1;
return pr;
}
// int main() {
// #ifndef ONLINE_JUDGE
// freopen("race.in", "r", stdin);
// freopen("race.out", "w", stdout);
// #endif
// cin.sync_with_stdio(0);
// cin.tie(0);
// int N, K;
// cin >> N >> K;
// int h[N - 1][2];
// int 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) << '\n';
// return 0;
// }
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |