#include <bits/stdc++.h>
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("-O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("-Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
using namespace std;
#ifndef wambule
#include "race.h"
#endif
inline void minfp(int);
long long gk;
struct vyv {
long long x;
int y;
map<long long, int> ma;
vyv() {
x = y = 0;
ma.clear();
}
void clc(long long a, int b) {
a -= x;
b -= y;
long long k = gk - x * 2;
if(ma.find(k - a) != ma.end()) {
minfp(ma[k - a] + y + b + y);
}
}
void add(long long a, int b) {
a -= x;
b -= y;
if(ma.find(a) == ma.end()) {
ma[a] = b;
} else {
ma[a] = min(ma[a], b);
}
}
void adc(long long a, int b) {
clc(a, b);
add(a, b);
}
void lft(int a, int b) {
x += a;
y += b;
}
};
const int N = 200005, K = 1000006;
int fp = -1, qz[N], ms[N], mw[N];
vector<pair<int, int>> v[N];
inline void minfp(int x) {
fp = (fp ^ -1 && fp < x ? fp : x);
}
void ovo(int dg = 0, int op = -1, vyv &a = *new vyv()) {
// cout << "[jent] " << dg << "\n";
if(!(op ^ -1 && v[dg].size() == 1)) {
ovo(ms[dg], dg, a);
a.lft(mw[dg], 1);
}
a.adc(0, 0);
for(auto p : v[dg]) {
if(p.first ^ op && p.first ^ ms[dg]) {
vyv x;
ovo(p.first, dg, x);
x.lft(p.second, 1);
for(auto it = x.ma.begin(); it != x.ma.end(); ++it) {
a.clc(it->first + x.x, it->second + x.y);
}
for(auto it = x.ma.begin(); it != x.ma.end(); ++it) {
a.add(it->first + x.x, it->second + x.y);
}
}
}
// cout << "[cont] " << dg << " " << "\n[^^^^] ";
// for(auto it = a.ma.begin(); it != a.ma.end(); ++it) {
// cout << "{" << it->first + a.x << ", " << it->second + a.y << "} ";
// }
// cout << "\n";
}
void omo(int dg = 0, int op = -1) {
// cout << "[plze] " << dg << "\n";
int mz = 0;
for(auto p : v[dg]) {
if(p.first ^ op) {
omo(p.first, dg);
qz[dg] += qz[p.first];
if(qz[p.first] > mz) {
mz = qz[p.first];
ms[dg] = p.first;
mw[dg] = p.second;
}
}
}
}
int best_path(int n, int k, int h[][2], int l[]) {
gk = k;
for(int i = 0; i < n; ++i) {
qz[i] = 1;
v[i].clear();
}
for(int i = 0; i < n - 1; ++i) {
v[h[i][0]].push_back({h[i][1], l[i]});
v[h[i][1]].push_back({h[i][0], l[i]});
}
omo();
ovo();
return fp;
}
#ifdef wambule
mt19937_64 rnd(time(0));
long long R(long long l, long long r) {
return 1ll * rnd() % (r - l + 1) + l;
}
long long Sr(long long r) {
return R(0, r - 1);
}
void G(int n, int h[][2]) {
vector<int> v;
for(int i = 0; i < n; ++i) {
v.push_back(i);
}
random_shuffle(v.begin(), v.end());
for(int i = 0; i < n - 1; ++i) {
h[i][0] = v[R(0, i)];
h[i][1] = v[i + 1];
}
}
int h[N][2];
int l[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
srand(time(0));
// cin.get();
////
int n = 4;
int k = 3;
int h[][2] = {0, 1, 1, 2, 1, 3};
int l[] = {1, 2, 4};
////
// int n = 11;
// int k = 12;
// int h[][2] = {0, 1, 0, 2, 2, 3, 3, 4, 4, 5, 0, 6, 6, 7, 6, 8, 8, 9, 8, 10};
// int l[] = {3, 4, 5, 4, 6, 3, 2, 5, 6, 7};
////
// int n = 200000;
// int k = 1000000;
// G(n, h);
// for(int i = 0; i < n - 1; ++i) {
// l[i] = R(0, 1000000);
// }
////
int rtnd = best_path(n, k, h, l);
cout << "[fnps] " << rtnd << endl;
cin.get();
return 0;
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
3 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
3 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
5 ms |
5100 KB |
Output is correct |
22 |
Correct |
5 ms |
5228 KB |
Output is correct |
23 |
Correct |
4 ms |
5228 KB |
Output is correct |
24 |
Correct |
5 ms |
5228 KB |
Output is correct |
25 |
Correct |
5 ms |
5228 KB |
Output is correct |
26 |
Correct |
5 ms |
5228 KB |
Output is correct |
27 |
Correct |
4 ms |
5100 KB |
Output is correct |
28 |
Correct |
5 ms |
5228 KB |
Output is correct |
29 |
Correct |
5 ms |
5228 KB |
Output is correct |
30 |
Correct |
6 ms |
5228 KB |
Output is correct |
31 |
Correct |
5 ms |
5228 KB |
Output is correct |
32 |
Correct |
6 ms |
5228 KB |
Output is correct |
33 |
Correct |
5 ms |
5228 KB |
Output is correct |
34 |
Correct |
5 ms |
5228 KB |
Output is correct |
35 |
Correct |
4 ms |
5228 KB |
Output is correct |
36 |
Correct |
4 ms |
5228 KB |
Output is correct |
37 |
Correct |
4 ms |
5228 KB |
Output is correct |
38 |
Correct |
5 ms |
5236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
3 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
118 ms |
12496 KB |
Output is correct |
20 |
Correct |
114 ms |
12524 KB |
Output is correct |
21 |
Correct |
112 ms |
12524 KB |
Output is correct |
22 |
Correct |
119 ms |
12544 KB |
Output is correct |
23 |
Correct |
138 ms |
13036 KB |
Output is correct |
24 |
Correct |
118 ms |
12800 KB |
Output is correct |
25 |
Correct |
101 ms |
25836 KB |
Output is correct |
26 |
Correct |
68 ms |
37100 KB |
Output is correct |
27 |
Correct |
218 ms |
20712 KB |
Output is correct |
28 |
Correct |
264 ms |
81516 KB |
Output is correct |
29 |
Correct |
278 ms |
78700 KB |
Output is correct |
30 |
Correct |
229 ms |
20716 KB |
Output is correct |
31 |
Correct |
215 ms |
20588 KB |
Output is correct |
32 |
Correct |
251 ms |
20716 KB |
Output is correct |
33 |
Correct |
224 ms |
20076 KB |
Output is correct |
34 |
Correct |
353 ms |
33516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
3 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
5 ms |
5100 KB |
Output is correct |
22 |
Correct |
5 ms |
5228 KB |
Output is correct |
23 |
Correct |
4 ms |
5228 KB |
Output is correct |
24 |
Correct |
5 ms |
5228 KB |
Output is correct |
25 |
Correct |
5 ms |
5228 KB |
Output is correct |
26 |
Correct |
5 ms |
5228 KB |
Output is correct |
27 |
Correct |
4 ms |
5100 KB |
Output is correct |
28 |
Correct |
5 ms |
5228 KB |
Output is correct |
29 |
Correct |
5 ms |
5228 KB |
Output is correct |
30 |
Correct |
6 ms |
5228 KB |
Output is correct |
31 |
Correct |
5 ms |
5228 KB |
Output is correct |
32 |
Correct |
6 ms |
5228 KB |
Output is correct |
33 |
Correct |
5 ms |
5228 KB |
Output is correct |
34 |
Correct |
5 ms |
5228 KB |
Output is correct |
35 |
Correct |
4 ms |
5228 KB |
Output is correct |
36 |
Correct |
4 ms |
5228 KB |
Output is correct |
37 |
Correct |
4 ms |
5228 KB |
Output is correct |
38 |
Correct |
5 ms |
5236 KB |
Output is correct |
39 |
Correct |
118 ms |
12496 KB |
Output is correct |
40 |
Correct |
114 ms |
12524 KB |
Output is correct |
41 |
Correct |
112 ms |
12524 KB |
Output is correct |
42 |
Correct |
119 ms |
12544 KB |
Output is correct |
43 |
Correct |
138 ms |
13036 KB |
Output is correct |
44 |
Correct |
118 ms |
12800 KB |
Output is correct |
45 |
Correct |
101 ms |
25836 KB |
Output is correct |
46 |
Correct |
68 ms |
37100 KB |
Output is correct |
47 |
Correct |
218 ms |
20712 KB |
Output is correct |
48 |
Correct |
264 ms |
81516 KB |
Output is correct |
49 |
Correct |
278 ms |
78700 KB |
Output is correct |
50 |
Correct |
229 ms |
20716 KB |
Output is correct |
51 |
Correct |
215 ms |
20588 KB |
Output is correct |
52 |
Correct |
251 ms |
20716 KB |
Output is correct |
53 |
Correct |
224 ms |
20076 KB |
Output is correct |
54 |
Correct |
353 ms |
33516 KB |
Output is correct |
55 |
Correct |
17 ms |
6252 KB |
Output is correct |
56 |
Correct |
10 ms |
5740 KB |
Output is correct |
57 |
Correct |
64 ms |
12268 KB |
Output is correct |
58 |
Correct |
56 ms |
11616 KB |
Output is correct |
59 |
Correct |
103 ms |
43116 KB |
Output is correct |
60 |
Correct |
263 ms |
79596 KB |
Output is correct |
61 |
Correct |
251 ms |
22380 KB |
Output is correct |
62 |
Correct |
208 ms |
20716 KB |
Output is correct |
63 |
Correct |
252 ms |
20716 KB |
Output is correct |
64 |
Correct |
482 ms |
27756 KB |
Output is correct |
65 |
Correct |
472 ms |
33772 KB |
Output is correct |
66 |
Correct |
294 ms |
72664 KB |
Output is correct |
67 |
Correct |
170 ms |
20956 KB |
Output is correct |
68 |
Correct |
360 ms |
31852 KB |
Output is correct |
69 |
Correct |
364 ms |
32092 KB |
Output is correct |
70 |
Correct |
333 ms |
30632 KB |
Output is correct |