#include<bits/stdc++.h>
#include "race.h"
using namespace std;
#define ll long long
#define ff first
#define ss second
#define ln "\n"
vector<pair<ll, pair<ll, ll>>> e;
vector<vector<ll>> A;
vector<bool> black;
vector<ll> sz;
ll n, k;
ll getsz(ll u, ll p){
sz[u]=1;
for (auto i:A[u]){
if (black[i]) continue;
ll v = e[i].ss.ff^e[i].ss.ss^u;
if (v==p) continue;
sz[u]+=getsz(v, u);
}
return sz[u];
}
void findcentroid(ll u, ll p, ll up, ll ¢r, ll wsz){
for (auto i:A[u]){
if (black[i]) continue;
ll v = e[i].ss.ff^e[i].ss.ss^u;
if (v==p) continue;
if (sz[v]>wsz/2) {findcentroid(v, u, up+1, centr, wsz); return;}
}
centr=u;
return;
}
void dfs(ll u, ll p, set<pair<ll, ll>> &usg, ll level, ll dep){
auto it = usg.lower_bound({level, -1});
if (usg.empty() or it==usg.end() or ((*it).ff==level and (*it).ss>dep) or (*it).ff!=level) {
if (!usg.empty() and it!=usg.end() and (*it).ff==level){
usg.erase(it);
}
usg.insert({level, dep});
}
for (auto i:A[u]){
ll v = e[i].ss.ff^e[i].ss.ss^u;
if (v==p or black[i]) continue;
dfs(v, u, usg, level+e[i].ff, dep+1);
}
}
void cedfs(ll u, ll &mn, ll wsz){
ll centr = -1;
findcentroid(u,u, 0, centr, wsz);
assert(centr!=-1);
set<pair<ll, ll>> levs, full;
for (auto i:A[u]){
if (black[i]) continue;
ll v = e[i].ss.ff^e[i].ss.ss^u;
levs.clear();
dfs(v, u, levs, e[i].ff, 1);
for (auto [length, tim]:levs){
auto it = full.lower_bound({k-length, -1});
if (!full.empty() and it!=full.end() and (*it).ff==k-length){
mn=min(mn, tim+(*it).ss);
}
if (length==k) {
mn=min(mn, tim);
}
}
// cout << ln;
for (auto [length, tim]:levs){
full.insert({length, tim});
}
}
for (auto i:A[u]){
if (black[i]) continue;
black[i]=1;
ll v = e[i].ss.ff^e[i].ss.ss^u;
cedfs(v, mn, getsz(v, v));
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N; k=K;
e.resize(N-1);
A.resize(N);
for (ll i=0; i<n-1; i++){
A[H[i][0]].push_back(i);
A[H[i][1]].push_back(i);
e[i] = {L[i], {H[i][0], H[i][1]}};
}
black.resize(N);
sz.resize(N);
ll mn = 2e18;
cedfs(0, mn, getsz(0, 0));
return (mn==2e18?-1:mn);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
5 ms |
1424 KB |
Output is correct |
23 |
Correct |
9 ms |
2396 KB |
Output is correct |
24 |
Correct |
4 ms |
1116 KB |
Output is correct |
25 |
Correct |
3 ms |
860 KB |
Output is correct |
26 |
Correct |
3 ms |
980 KB |
Output is correct |
27 |
Correct |
1 ms |
560 KB |
Output is correct |
28 |
Correct |
4 ms |
860 KB |
Output is correct |
29 |
Correct |
2 ms |
1112 KB |
Output is correct |
30 |
Correct |
3 ms |
860 KB |
Output is correct |
31 |
Correct |
3 ms |
860 KB |
Output is correct |
32 |
Correct |
3 ms |
860 KB |
Output is correct |
33 |
Correct |
4 ms |
1116 KB |
Output is correct |
34 |
Correct |
29 ms |
12644 KB |
Output is correct |
35 |
Correct |
37 ms |
15960 KB |
Output is correct |
36 |
Correct |
41 ms |
20052 KB |
Output is correct |
37 |
Correct |
16 ms |
6748 KB |
Output is correct |
38 |
Correct |
13 ms |
4188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
456 ms |
11600 KB |
Output is correct |
20 |
Correct |
449 ms |
12604 KB |
Output is correct |
21 |
Correct |
342 ms |
12040 KB |
Output is correct |
22 |
Correct |
245 ms |
12488 KB |
Output is correct |
23 |
Correct |
306 ms |
14608 KB |
Output is correct |
24 |
Correct |
137 ms |
12520 KB |
Output is correct |
25 |
Runtime error |
1848 ms |
262144 KB |
Execution killed with signal 9 |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
5 ms |
1424 KB |
Output is correct |
23 |
Correct |
9 ms |
2396 KB |
Output is correct |
24 |
Correct |
4 ms |
1116 KB |
Output is correct |
25 |
Correct |
3 ms |
860 KB |
Output is correct |
26 |
Correct |
3 ms |
980 KB |
Output is correct |
27 |
Correct |
1 ms |
560 KB |
Output is correct |
28 |
Correct |
4 ms |
860 KB |
Output is correct |
29 |
Correct |
2 ms |
1112 KB |
Output is correct |
30 |
Correct |
3 ms |
860 KB |
Output is correct |
31 |
Correct |
3 ms |
860 KB |
Output is correct |
32 |
Correct |
3 ms |
860 KB |
Output is correct |
33 |
Correct |
4 ms |
1116 KB |
Output is correct |
34 |
Correct |
29 ms |
12644 KB |
Output is correct |
35 |
Correct |
37 ms |
15960 KB |
Output is correct |
36 |
Correct |
41 ms |
20052 KB |
Output is correct |
37 |
Correct |
16 ms |
6748 KB |
Output is correct |
38 |
Correct |
13 ms |
4188 KB |
Output is correct |
39 |
Correct |
456 ms |
11600 KB |
Output is correct |
40 |
Correct |
449 ms |
12604 KB |
Output is correct |
41 |
Correct |
342 ms |
12040 KB |
Output is correct |
42 |
Correct |
245 ms |
12488 KB |
Output is correct |
43 |
Correct |
306 ms |
14608 KB |
Output is correct |
44 |
Correct |
137 ms |
12520 KB |
Output is correct |
45 |
Runtime error |
1848 ms |
262144 KB |
Execution killed with signal 9 |
46 |
Halted |
0 ms |
0 KB |
- |