#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10, inf=1e9;
int n, k, tin[N], tout[N], tdfs, tour[N];
vector<pair<int, int>> g[N];
int dep[N], dist[N], sz[N];
map<int, int> mp;
int ans;
void dfs_sz(int u, int p){
tin[u]=++tdfs;
tour[tdfs]=u;
for (int i=0; i<(int)g[u].size(); ++i){
if (g[u][i].first==p){
g[u].erase(g[u].begin()+i);
break;
}
}
sz[u]=1;
for (auto &e:g[u]){
int v=e.first, w=e.second;
dep[v]=dep[u]+1;
dist[v]=dist[u]+w;
dfs_sz(v, u);
sz[u]+=sz[v];
if (sz[v]>sz[g[u][0].first]) swap(e, g[u][0]);
}
tout[u]=tdfs;
}
void minimize(int x, int y){
auto it=mp.find(x);
if (it==mp.end()) mp[x]=y;
else it->second=min(it->second, y);
}
int get(int x){
auto it=mp.find(x);
if (it==mp.end()) return inf;
return it->second;
}
void dfs_merge(int u, bool keep){
if (g[u].empty()){
if (keep) minimize(dist[u], dep[u]);
return;
}
for (auto &e:g[u]) if (e!=g[u][0]) dfs_merge(e.first, 0);
dfs_merge(g[u][0].first, 1);
for (auto &e:g[u]) if (e!=g[u][0]){
int v=e.first;
for (int i=tin[v]; i<=tout[v]; ++i){
int w=tour[i];
ans=min(ans, get(k-dist[w]+dist[u]*2)+dep[w]-dep[u]*2);
}
for (int i=tin[v]; i<=tout[v]; ++i){
int w=tour[i];
minimize(dist[w], dep[w]);
}
}
ans=min(ans, get(k+dist[u])-dep[u]);
minimize(dist[u], dep[u]);
if (!keep) mp.clear();
}
int32_t best_path(int32_t _N, int32_t K, int32_t H[][2], int32_t L[])
{
n=_N, k=K;
for (int i=0; i<n-1; ++i){
int u=H[i][0]+1, v=H[i][1]+1, w=L[i];
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
dfs_sz(1, 0);
ans=inf;
dfs_merge(1, 0);
return ans>n?-1:ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10584 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
4 ms |
10584 KB |
Output is correct |
16 |
Correct |
2 ms |
10596 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10584 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
4 ms |
10584 KB |
Output is correct |
16 |
Correct |
2 ms |
10596 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10584 KB |
Output is correct |
19 |
Correct |
2 ms |
10840 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10844 KB |
Output is correct |
23 |
Correct |
3 ms |
10844 KB |
Output is correct |
24 |
Correct |
3 ms |
10676 KB |
Output is correct |
25 |
Correct |
3 ms |
10844 KB |
Output is correct |
26 |
Correct |
3 ms |
10844 KB |
Output is correct |
27 |
Correct |
2 ms |
10584 KB |
Output is correct |
28 |
Correct |
3 ms |
10844 KB |
Output is correct |
29 |
Correct |
3 ms |
10844 KB |
Output is correct |
30 |
Correct |
4 ms |
10844 KB |
Output is correct |
31 |
Correct |
3 ms |
10844 KB |
Output is correct |
32 |
Correct |
3 ms |
10844 KB |
Output is correct |
33 |
Correct |
3 ms |
10840 KB |
Output is correct |
34 |
Correct |
2 ms |
10840 KB |
Output is correct |
35 |
Correct |
2 ms |
10844 KB |
Output is correct |
36 |
Correct |
2 ms |
10844 KB |
Output is correct |
37 |
Correct |
2 ms |
10844 KB |
Output is correct |
38 |
Correct |
3 ms |
10700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10584 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
4 ms |
10584 KB |
Output is correct |
16 |
Correct |
2 ms |
10596 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10584 KB |
Output is correct |
19 |
Correct |
92 ms |
19952 KB |
Output is correct |
20 |
Correct |
94 ms |
19952 KB |
Output is correct |
21 |
Correct |
93 ms |
19968 KB |
Output is correct |
22 |
Correct |
83 ms |
19820 KB |
Output is correct |
23 |
Correct |
107 ms |
20308 KB |
Output is correct |
24 |
Correct |
76 ms |
19576 KB |
Output is correct |
25 |
Correct |
61 ms |
26452 KB |
Output is correct |
26 |
Correct |
51 ms |
31872 KB |
Output is correct |
27 |
Correct |
118 ms |
30552 KB |
Output is correct |
28 |
Correct |
217 ms |
66388 KB |
Output is correct |
29 |
Correct |
225 ms |
64532 KB |
Output is correct |
30 |
Correct |
132 ms |
30548 KB |
Output is correct |
31 |
Correct |
124 ms |
30548 KB |
Output is correct |
32 |
Correct |
177 ms |
30480 KB |
Output is correct |
33 |
Correct |
134 ms |
29524 KB |
Output is correct |
34 |
Correct |
271 ms |
41556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10584 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
4 ms |
10584 KB |
Output is correct |
16 |
Correct |
2 ms |
10596 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10584 KB |
Output is correct |
19 |
Correct |
2 ms |
10840 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10844 KB |
Output is correct |
23 |
Correct |
3 ms |
10844 KB |
Output is correct |
24 |
Correct |
3 ms |
10676 KB |
Output is correct |
25 |
Correct |
3 ms |
10844 KB |
Output is correct |
26 |
Correct |
3 ms |
10844 KB |
Output is correct |
27 |
Correct |
2 ms |
10584 KB |
Output is correct |
28 |
Correct |
3 ms |
10844 KB |
Output is correct |
29 |
Correct |
3 ms |
10844 KB |
Output is correct |
30 |
Correct |
4 ms |
10844 KB |
Output is correct |
31 |
Correct |
3 ms |
10844 KB |
Output is correct |
32 |
Correct |
3 ms |
10844 KB |
Output is correct |
33 |
Correct |
3 ms |
10840 KB |
Output is correct |
34 |
Correct |
2 ms |
10840 KB |
Output is correct |
35 |
Correct |
2 ms |
10844 KB |
Output is correct |
36 |
Correct |
2 ms |
10844 KB |
Output is correct |
37 |
Correct |
2 ms |
10844 KB |
Output is correct |
38 |
Correct |
3 ms |
10700 KB |
Output is correct |
39 |
Correct |
92 ms |
19952 KB |
Output is correct |
40 |
Correct |
94 ms |
19952 KB |
Output is correct |
41 |
Correct |
93 ms |
19968 KB |
Output is correct |
42 |
Correct |
83 ms |
19820 KB |
Output is correct |
43 |
Correct |
107 ms |
20308 KB |
Output is correct |
44 |
Correct |
76 ms |
19576 KB |
Output is correct |
45 |
Correct |
61 ms |
26452 KB |
Output is correct |
46 |
Correct |
51 ms |
31872 KB |
Output is correct |
47 |
Correct |
118 ms |
30552 KB |
Output is correct |
48 |
Correct |
217 ms |
66388 KB |
Output is correct |
49 |
Correct |
225 ms |
64532 KB |
Output is correct |
50 |
Correct |
132 ms |
30548 KB |
Output is correct |
51 |
Correct |
124 ms |
30548 KB |
Output is correct |
52 |
Correct |
177 ms |
30480 KB |
Output is correct |
53 |
Correct |
134 ms |
29524 KB |
Output is correct |
54 |
Correct |
271 ms |
41556 KB |
Output is correct |
55 |
Correct |
11 ms |
11868 KB |
Output is correct |
56 |
Correct |
7 ms |
11356 KB |
Output is correct |
57 |
Correct |
58 ms |
20436 KB |
Output is correct |
58 |
Correct |
34 ms |
20496 KB |
Output is correct |
59 |
Correct |
68 ms |
39216 KB |
Output is correct |
60 |
Correct |
212 ms |
66960 KB |
Output is correct |
61 |
Correct |
135 ms |
33720 KB |
Output is correct |
62 |
Correct |
116 ms |
32484 KB |
Output is correct |
63 |
Correct |
176 ms |
32672 KB |
Output is correct |
64 |
Correct |
378 ms |
36868 KB |
Output is correct |
65 |
Correct |
385 ms |
45216 KB |
Output is correct |
66 |
Correct |
226 ms |
60544 KB |
Output is correct |
67 |
Correct |
105 ms |
32496 KB |
Output is correct |
68 |
Correct |
243 ms |
42644 KB |
Output is correct |
69 |
Correct |
250 ms |
40572 KB |
Output is correct |
70 |
Correct |
231 ms |
41532 KB |
Output is correct |