/*
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;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
512 KB |
Output is correct |
22 |
Correct |
3 ms |
512 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
512 KB |
Output is correct |
26 |
Correct |
3 ms |
512 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
3 ms |
512 KB |
Output is correct |
29 |
Correct |
3 ms |
512 KB |
Output is correct |
30 |
Correct |
3 ms |
512 KB |
Output is correct |
31 |
Correct |
3 ms |
512 KB |
Output is correct |
32 |
Correct |
4 ms |
512 KB |
Output is correct |
33 |
Correct |
4 ms |
512 KB |
Output is correct |
34 |
Correct |
3 ms |
512 KB |
Output is correct |
35 |
Correct |
3 ms |
512 KB |
Output is correct |
36 |
Correct |
3 ms |
512 KB |
Output is correct |
37 |
Correct |
3 ms |
512 KB |
Output is correct |
38 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
430 ms |
14812 KB |
Output is correct |
20 |
Correct |
429 ms |
14752 KB |
Output is correct |
21 |
Correct |
405 ms |
14632 KB |
Output is correct |
22 |
Correct |
356 ms |
14676 KB |
Output is correct |
23 |
Correct |
561 ms |
15080 KB |
Output is correct |
24 |
Correct |
256 ms |
13688 KB |
Output is correct |
25 |
Correct |
307 ms |
18320 KB |
Output is correct |
26 |
Correct |
177 ms |
22568 KB |
Output is correct |
27 |
Correct |
488 ms |
27512 KB |
Output is correct |
28 |
Correct |
658 ms |
43640 KB |
Output is correct |
29 |
Correct |
652 ms |
42360 KB |
Output is correct |
30 |
Correct |
488 ms |
27516 KB |
Output is correct |
31 |
Correct |
492 ms |
27512 KB |
Output is correct |
32 |
Correct |
568 ms |
27512 KB |
Output is correct |
33 |
Correct |
873 ms |
28384 KB |
Output is correct |
34 |
Correct |
352 ms |
28128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
512 KB |
Output is correct |
22 |
Correct |
3 ms |
512 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
512 KB |
Output is correct |
26 |
Correct |
3 ms |
512 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
3 ms |
512 KB |
Output is correct |
29 |
Correct |
3 ms |
512 KB |
Output is correct |
30 |
Correct |
3 ms |
512 KB |
Output is correct |
31 |
Correct |
3 ms |
512 KB |
Output is correct |
32 |
Correct |
4 ms |
512 KB |
Output is correct |
33 |
Correct |
4 ms |
512 KB |
Output is correct |
34 |
Correct |
3 ms |
512 KB |
Output is correct |
35 |
Correct |
3 ms |
512 KB |
Output is correct |
36 |
Correct |
3 ms |
512 KB |
Output is correct |
37 |
Correct |
3 ms |
512 KB |
Output is correct |
38 |
Correct |
3 ms |
512 KB |
Output is correct |
39 |
Correct |
430 ms |
14812 KB |
Output is correct |
40 |
Correct |
429 ms |
14752 KB |
Output is correct |
41 |
Correct |
405 ms |
14632 KB |
Output is correct |
42 |
Correct |
356 ms |
14676 KB |
Output is correct |
43 |
Correct |
561 ms |
15080 KB |
Output is correct |
44 |
Correct |
256 ms |
13688 KB |
Output is correct |
45 |
Correct |
307 ms |
18320 KB |
Output is correct |
46 |
Correct |
177 ms |
22568 KB |
Output is correct |
47 |
Correct |
488 ms |
27512 KB |
Output is correct |
48 |
Correct |
658 ms |
43640 KB |
Output is correct |
49 |
Correct |
652 ms |
42360 KB |
Output is correct |
50 |
Correct |
488 ms |
27516 KB |
Output is correct |
51 |
Correct |
492 ms |
27512 KB |
Output is correct |
52 |
Correct |
568 ms |
27512 KB |
Output is correct |
53 |
Correct |
873 ms |
28384 KB |
Output is correct |
54 |
Correct |
352 ms |
28128 KB |
Output is correct |
55 |
Correct |
37 ms |
2232 KB |
Output is correct |
56 |
Correct |
23 ms |
1792 KB |
Output is correct |
57 |
Correct |
250 ms |
15084 KB |
Output is correct |
58 |
Correct |
67 ms |
13292 KB |
Output is correct |
59 |
Correct |
598 ms |
28868 KB |
Output is correct |
60 |
Correct |
1701 ms |
59924 KB |
Output is correct |
61 |
Correct |
636 ms |
30072 KB |
Output is correct |
62 |
Correct |
561 ms |
27824 KB |
Output is correct |
63 |
Correct |
705 ms |
27880 KB |
Output is correct |
64 |
Correct |
1692 ms |
38136 KB |
Output is correct |
65 |
Correct |
424 ms |
28792 KB |
Output is correct |
66 |
Correct |
1515 ms |
44100 KB |
Output is correct |
67 |
Correct |
253 ms |
27500 KB |
Output is correct |
68 |
Correct |
905 ms |
44548 KB |
Output is correct |
69 |
Correct |
915 ms |
44916 KB |
Output is correct |
70 |
Correct |
860 ms |
42432 KB |
Output is correct |