// #include <bits/stdc++.h>
// using namespace std;
// #define s second
// #define f first
// #define pb push_back
// typedef long long ll;
// typedef pair<ll, ll> pii;
// typedef vector<pii> vpii;
// typedef vector<ll> vi;
// #define FOR(i, a, b) for (ll i = (a); i<b; i++)
// const ll MN = 3e5 + 10;
// vpii adj[MN];
// ll d[MN]; // from root in highways
// ll d2[MN]; // from root in distance
// ll k;
// map<ll, ll> small[MN];
// void dfs(ll cur = 0, ll dist = 0, ll dist2 = 0, ll p = -1) {
// // cout << cur << endl;
// d[cur]=dist;
// d2[cur]=dist2;
// small[cur][dist2]=dist;
// for (auto val: adj[cur]) {
// if (val.f != p) {
// dfs(val.f, dist + 1ll, dist2 + val.s, cur);
// }
// }
// }
// ll ans = 1e9;
// void solve(ll cur, ll p=-1) {
// ll target = k + 2*d2[cur];
// for (auto val: adj[cur]) {
// if (val.f != p) {
// solve(val.f, cur);
// if (small[val.f].size() > small[cur].size()) swap(small[val.f], small[cur]);
// for (pii nx: small[val.f]) {
// if (small[cur].find(target - nx.f) != small[cur].end()) {
// ans = min(ans, small[cur][target - nx.f] + nx.s - 2*d[cur]);
// }
// }
// for (pii nx: small[val.f]) {
// if (small[cur].find(nx.f) == small[cur].end()) small[cur].insert(nx);
// else small[cur][nx.f]=min(small[cur][nx.f], nx.s);
// }
// }
// }
// }
// int best_path(int N, int K, int H[][2], int L[])
// {
// k = K;
// FOR(i, 0, N-1) {
// adj[H[i][0]].pb({H[i][1], L[i]});
// }
// dfs();
// solve(0);
// if (ans == 1e9) ans = -1;
// return ans;
// }
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
#define pb push_back
#define f first
#define s second
const int maxn = 2e5 + 1;
vector<pii> adj[maxn];
map<ll, ll> info[maxn];
ll dist[maxn], sum[maxn]; // define these relative to root
int N, K;
ll ret;
void precomp(int u, int p, ll c, int h) {
info[u][c] = h;
sum[u] = c;
dist[u] = h;
for (auto n : adj[u]) {
if (p == n.f) { continue; }
precomp(n.f, u, c + n.s, h + 1);
}
}
void small_to_large_merge(int u, int p) {
ll target = K + 2 * sum[u];
for (auto n : adj[u]) {
if (n.f == p) { continue; }
small_to_large_merge(n.f, u);
if (info[n.f].size() > info[u].size()) { swap(info[n.f], info[u]); }
for (auto i : info[n.f]) {
if (info[u].find(target - i.f) != info[u].end()) {
ret = min(ret, info[u][target - i.f] + i.s - 2 * dist[u]);
}
}
for (auto i : info[n.f]) {
if (info[u].find(i.f) == info[u].end()) {
info[u].insert(i);
} else {
info[u][i.f] = min(info[u][i.f], i.s);
}
}
}
}
int best_path(int n, int k, int edges[][2], int weights[]) {
if (k == 1) { return 0; }
N = n;
K = k;
ret = INT_MAX;
for (int i = 0; i < n - 1; i++) {
int u = edges[i][0];
int v = edges[i][1];
adj[u].pb(pii(v, weights[i]));
adj[v].pb(pii(u, weights[i]));
}
precomp(0, -1, 0, 0);
small_to_large_merge(0, -1);
return ret == INT_MAX ? -1 : ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14440 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14360 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14456 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14420 KB |
Output is correct |
11 |
Correct |
9 ms |
14452 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14416 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14440 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14360 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14456 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14420 KB |
Output is correct |
11 |
Correct |
9 ms |
14452 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14416 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
20 |
Correct |
9 ms |
14432 KB |
Output is correct |
21 |
Correct |
10 ms |
14632 KB |
Output is correct |
22 |
Correct |
9 ms |
14676 KB |
Output is correct |
23 |
Correct |
11 ms |
14668 KB |
Output is correct |
24 |
Correct |
10 ms |
14676 KB |
Output is correct |
25 |
Correct |
8 ms |
14676 KB |
Output is correct |
26 |
Correct |
9 ms |
14704 KB |
Output is correct |
27 |
Correct |
8 ms |
14548 KB |
Output is correct |
28 |
Correct |
10 ms |
14664 KB |
Output is correct |
29 |
Correct |
9 ms |
14668 KB |
Output is correct |
30 |
Correct |
9 ms |
14668 KB |
Output is correct |
31 |
Correct |
10 ms |
14716 KB |
Output is correct |
32 |
Correct |
9 ms |
14672 KB |
Output is correct |
33 |
Correct |
10 ms |
14728 KB |
Output is correct |
34 |
Correct |
10 ms |
14676 KB |
Output is correct |
35 |
Correct |
9 ms |
14628 KB |
Output is correct |
36 |
Correct |
9 ms |
14664 KB |
Output is correct |
37 |
Correct |
9 ms |
14584 KB |
Output is correct |
38 |
Correct |
9 ms |
14680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14440 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14360 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14456 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14420 KB |
Output is correct |
11 |
Correct |
9 ms |
14452 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14416 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
145 ms |
37808 KB |
Output is correct |
20 |
Correct |
167 ms |
39120 KB |
Output is correct |
21 |
Correct |
143 ms |
38948 KB |
Output is correct |
22 |
Correct |
145 ms |
38548 KB |
Output is correct |
23 |
Correct |
214 ms |
51324 KB |
Output is correct |
24 |
Correct |
145 ms |
41344 KB |
Output is correct |
25 |
Correct |
133 ms |
39956 KB |
Output is correct |
26 |
Correct |
74 ms |
48240 KB |
Output is correct |
27 |
Correct |
234 ms |
52576 KB |
Output is correct |
28 |
Correct |
361 ms |
93936 KB |
Output is correct |
29 |
Correct |
359 ms |
92980 KB |
Output is correct |
30 |
Correct |
229 ms |
52704 KB |
Output is correct |
31 |
Correct |
225 ms |
52584 KB |
Output is correct |
32 |
Correct |
316 ms |
52752 KB |
Output is correct |
33 |
Correct |
298 ms |
57548 KB |
Output is correct |
34 |
Correct |
453 ms |
90304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14440 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14360 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14456 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14420 KB |
Output is correct |
11 |
Correct |
9 ms |
14452 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14416 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
20 |
Correct |
9 ms |
14432 KB |
Output is correct |
21 |
Correct |
10 ms |
14632 KB |
Output is correct |
22 |
Correct |
9 ms |
14676 KB |
Output is correct |
23 |
Correct |
11 ms |
14668 KB |
Output is correct |
24 |
Correct |
10 ms |
14676 KB |
Output is correct |
25 |
Correct |
8 ms |
14676 KB |
Output is correct |
26 |
Correct |
9 ms |
14704 KB |
Output is correct |
27 |
Correct |
8 ms |
14548 KB |
Output is correct |
28 |
Correct |
10 ms |
14664 KB |
Output is correct |
29 |
Correct |
9 ms |
14668 KB |
Output is correct |
30 |
Correct |
9 ms |
14668 KB |
Output is correct |
31 |
Correct |
10 ms |
14716 KB |
Output is correct |
32 |
Correct |
9 ms |
14672 KB |
Output is correct |
33 |
Correct |
10 ms |
14728 KB |
Output is correct |
34 |
Correct |
10 ms |
14676 KB |
Output is correct |
35 |
Correct |
9 ms |
14628 KB |
Output is correct |
36 |
Correct |
9 ms |
14664 KB |
Output is correct |
37 |
Correct |
9 ms |
14584 KB |
Output is correct |
38 |
Correct |
9 ms |
14680 KB |
Output is correct |
39 |
Correct |
145 ms |
37808 KB |
Output is correct |
40 |
Correct |
167 ms |
39120 KB |
Output is correct |
41 |
Correct |
143 ms |
38948 KB |
Output is correct |
42 |
Correct |
145 ms |
38548 KB |
Output is correct |
43 |
Correct |
214 ms |
51324 KB |
Output is correct |
44 |
Correct |
145 ms |
41344 KB |
Output is correct |
45 |
Correct |
133 ms |
39956 KB |
Output is correct |
46 |
Correct |
74 ms |
48240 KB |
Output is correct |
47 |
Correct |
234 ms |
52576 KB |
Output is correct |
48 |
Correct |
361 ms |
93936 KB |
Output is correct |
49 |
Correct |
359 ms |
92980 KB |
Output is correct |
50 |
Correct |
229 ms |
52704 KB |
Output is correct |
51 |
Correct |
225 ms |
52584 KB |
Output is correct |
52 |
Correct |
316 ms |
52752 KB |
Output is correct |
53 |
Correct |
298 ms |
57548 KB |
Output is correct |
54 |
Correct |
453 ms |
90304 KB |
Output is correct |
55 |
Correct |
22 ms |
17752 KB |
Output is correct |
56 |
Correct |
16 ms |
16516 KB |
Output is correct |
57 |
Correct |
80 ms |
36756 KB |
Output is correct |
58 |
Correct |
57 ms |
29184 KB |
Output is correct |
59 |
Correct |
109 ms |
54552 KB |
Output is correct |
60 |
Correct |
367 ms |
93492 KB |
Output is correct |
61 |
Correct |
251 ms |
55852 KB |
Output is correct |
62 |
Correct |
219 ms |
52752 KB |
Output is correct |
63 |
Correct |
314 ms |
52728 KB |
Output is correct |
64 |
Correct |
493 ms |
107516 KB |
Output is correct |
65 |
Correct |
472 ms |
105616 KB |
Output is correct |
66 |
Correct |
347 ms |
88908 KB |
Output is correct |
67 |
Correct |
213 ms |
45488 KB |
Output is correct |
68 |
Correct |
389 ms |
69052 KB |
Output is correct |
69 |
Correct |
409 ms |
73884 KB |
Output is correct |
70 |
Correct |
405 ms |
64600 KB |
Output is correct |