#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define REP(i, a, b) for(int i = a; i >= b; --i)
#define left _________left
#define right _________right
#define NAME ""
const int mod = 1e9 + 7;
bool maximize(int &u, int v){
if(v > u){
u = v;
return true;
}
return false;
}
bool minimize(int &u, int v){
if(v < u){
u = v;
return true;
}
return false;
}
bool maximizell(long long &u, long long v){
if(v > u){
u = v;
return true;
}
return false;
}
bool minimizell(long long &u, long long v){
if(v < u){
u = v;
return true;
}
return false;
}
int fastPow(int a, int n){
if(n == 0) return 1;
int t = fastPow(a, n >> 1);
t = 1ll * t * t % mod;
if(n & 1) t = 1ll * t * a % mod;
return t;
}
void add(int &u, int v){
u += v;
if(u >= mod) u -= mod;
}
void sub(int &u, int v){
u -= v;
if(u < 0) u += mod;
}
const int maxN = 2e5 + 5;
const int maxK = 1e6 + 5;
const int INF = 1e9;
vector<pair<int, int>> adj[maxN];
int n, m, k;
int answer = INF, sz[maxN], in[maxN], out[maxN], timeDFS, id[maxN], heavy[maxN], depth[maxN];
int high[maxN];
map<int, int> Min;
inline void dfs(int u){
in[u] = ++timeDFS;
id[timeDFS] = u;
sz[u] = 1;
for(auto[v, w] : adj[u]){
if(in[v])continue;
high[v] = high[u] + 1;
depth[v] = depth[u] + w;
dfs(v);
sz[u] += sz[v];
if(sz[v] > sz[heavy[u]]) heavy[u] = v;
}
out[u] = timeDFS;
}
inline void hld(int u = 1, int pre = 0, bool keep = true){
for(auto[v, w] : adj[u])if(v != heavy[u] && v != pre){
hld(v, u, false);
FOR(i, in[v], out[v]){
Min.erase(depth[id[i]]);
}
}
if(heavy[u]) hld(heavy[u], u, true);
if(Min.find(depth[u]) == Min.end()) Min[depth[u]] = high[u];
else minimize(Min[depth[u]], high[u]);
if(Min.find(k + depth[u]) != Min.end()) minimize(answer, Min[depth[u] + k] - high[u]);
for(auto[v, w] : adj[u]){
if(v != heavy[u] && v != pre){
FOR(i, in[v], out[v]){
int c = id[i], tmp = depth[c] - 2 * depth[u];
if(Min.find(k - tmp) != Min.end()){
minimize(answer, high[c] - 2 * high[u] + Min[k - tmp]);
}
}
FOR(i, in[v], out[v]){
if(Min.find(depth[id[i]]) == Min.end()) Min[depth[id[i]]] = high[id[i]];
else minimize(Min[depth[id[i]]], high[id[i]]);
}
}
}
}
int best_path(int N, int K, int H[][2], int L[]){
n = N, k = K;
for(int i = 0; i < n - 1; ++i){
int u = H[i][0], v = H[i][1], w = L[i];
++u;
++v;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
dfs(1);
hld();
return answer == INF ? -1 : answer;
}
//int H[maxN][2], L[maxN];
//inline void solve(){
// cin >> n >> k;
// FOR(i, 0, n - 2){
// int u, v, w;
// cin >> u >> v >> w;
// H[i][0] = u;
// H[i][1] = v;
// L[i] = w;
//
// }
// cout << best_path(n, k, H, L);
//}
//int main(){
// if(fopen(NAME".inp", "r")){
// freopen(NAME".inp", "r", stdin);
// freopen(NAME".out", "w", stdout);
// }
// ios_base::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
//
// int t = 1;
//// cin >> t;
// while(t--){
// solve();
// }
// return 0;
//}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12112 KB |
Output is correct |
2 |
Correct |
3 ms |
12208 KB |
Output is correct |
3 |
Correct |
3 ms |
12112 KB |
Output is correct |
4 |
Correct |
3 ms |
12204 KB |
Output is correct |
5 |
Correct |
3 ms |
12112 KB |
Output is correct |
6 |
Correct |
3 ms |
12112 KB |
Output is correct |
7 |
Correct |
3 ms |
12112 KB |
Output is correct |
8 |
Correct |
3 ms |
12112 KB |
Output is correct |
9 |
Correct |
3 ms |
12112 KB |
Output is correct |
10 |
Correct |
3 ms |
12112 KB |
Output is correct |
11 |
Correct |
4 ms |
12112 KB |
Output is correct |
12 |
Correct |
3 ms |
12112 KB |
Output is correct |
13 |
Correct |
3 ms |
12112 KB |
Output is correct |
14 |
Correct |
3 ms |
12112 KB |
Output is correct |
15 |
Correct |
3 ms |
12112 KB |
Output is correct |
16 |
Correct |
3 ms |
12112 KB |
Output is correct |
17 |
Correct |
3 ms |
12112 KB |
Output is correct |
18 |
Correct |
3 ms |
12112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12112 KB |
Output is correct |
2 |
Correct |
3 ms |
12208 KB |
Output is correct |
3 |
Correct |
3 ms |
12112 KB |
Output is correct |
4 |
Correct |
3 ms |
12204 KB |
Output is correct |
5 |
Correct |
3 ms |
12112 KB |
Output is correct |
6 |
Correct |
3 ms |
12112 KB |
Output is correct |
7 |
Correct |
3 ms |
12112 KB |
Output is correct |
8 |
Correct |
3 ms |
12112 KB |
Output is correct |
9 |
Correct |
3 ms |
12112 KB |
Output is correct |
10 |
Correct |
3 ms |
12112 KB |
Output is correct |
11 |
Correct |
4 ms |
12112 KB |
Output is correct |
12 |
Correct |
3 ms |
12112 KB |
Output is correct |
13 |
Correct |
3 ms |
12112 KB |
Output is correct |
14 |
Correct |
3 ms |
12112 KB |
Output is correct |
15 |
Correct |
3 ms |
12112 KB |
Output is correct |
16 |
Correct |
3 ms |
12112 KB |
Output is correct |
17 |
Correct |
3 ms |
12112 KB |
Output is correct |
18 |
Correct |
3 ms |
12112 KB |
Output is correct |
19 |
Correct |
3 ms |
12112 KB |
Output is correct |
20 |
Correct |
3 ms |
12112 KB |
Output is correct |
21 |
Correct |
3 ms |
12112 KB |
Output is correct |
22 |
Correct |
3 ms |
12112 KB |
Output is correct |
23 |
Correct |
3 ms |
12112 KB |
Output is correct |
24 |
Correct |
4 ms |
12112 KB |
Output is correct |
25 |
Correct |
4 ms |
12112 KB |
Output is correct |
26 |
Correct |
3 ms |
12112 KB |
Output is correct |
27 |
Correct |
3 ms |
12112 KB |
Output is correct |
28 |
Correct |
3 ms |
12112 KB |
Output is correct |
29 |
Correct |
3 ms |
12112 KB |
Output is correct |
30 |
Correct |
3 ms |
12216 KB |
Output is correct |
31 |
Correct |
3 ms |
12112 KB |
Output is correct |
32 |
Correct |
3 ms |
12112 KB |
Output is correct |
33 |
Correct |
4 ms |
12112 KB |
Output is correct |
34 |
Correct |
3 ms |
12112 KB |
Output is correct |
35 |
Correct |
3 ms |
12368 KB |
Output is correct |
36 |
Correct |
3 ms |
12368 KB |
Output is correct |
37 |
Correct |
3 ms |
12112 KB |
Output is correct |
38 |
Correct |
3 ms |
12112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12112 KB |
Output is correct |
2 |
Correct |
3 ms |
12208 KB |
Output is correct |
3 |
Correct |
3 ms |
12112 KB |
Output is correct |
4 |
Correct |
3 ms |
12204 KB |
Output is correct |
5 |
Correct |
3 ms |
12112 KB |
Output is correct |
6 |
Correct |
3 ms |
12112 KB |
Output is correct |
7 |
Correct |
3 ms |
12112 KB |
Output is correct |
8 |
Correct |
3 ms |
12112 KB |
Output is correct |
9 |
Correct |
3 ms |
12112 KB |
Output is correct |
10 |
Correct |
3 ms |
12112 KB |
Output is correct |
11 |
Correct |
4 ms |
12112 KB |
Output is correct |
12 |
Correct |
3 ms |
12112 KB |
Output is correct |
13 |
Correct |
3 ms |
12112 KB |
Output is correct |
14 |
Correct |
3 ms |
12112 KB |
Output is correct |
15 |
Correct |
3 ms |
12112 KB |
Output is correct |
16 |
Correct |
3 ms |
12112 KB |
Output is correct |
17 |
Correct |
3 ms |
12112 KB |
Output is correct |
18 |
Correct |
3 ms |
12112 KB |
Output is correct |
19 |
Correct |
83 ms |
20304 KB |
Output is correct |
20 |
Correct |
81 ms |
19120 KB |
Output is correct |
21 |
Correct |
81 ms |
19036 KB |
Output is correct |
22 |
Correct |
79 ms |
20576 KB |
Output is correct |
23 |
Correct |
120 ms |
20836 KB |
Output is correct |
24 |
Correct |
84 ms |
20560 KB |
Output is correct |
25 |
Correct |
59 ms |
28204 KB |
Output is correct |
26 |
Correct |
42 ms |
35600 KB |
Output is correct |
27 |
Correct |
105 ms |
26940 KB |
Output is correct |
28 |
Correct |
186 ms |
66376 KB |
Output is correct |
29 |
Correct |
168 ms |
60756 KB |
Output is correct |
30 |
Correct |
104 ms |
27056 KB |
Output is correct |
31 |
Correct |
107 ms |
27168 KB |
Output is correct |
32 |
Correct |
123 ms |
27260 KB |
Output is correct |
33 |
Correct |
144 ms |
26184 KB |
Output is correct |
34 |
Correct |
252 ms |
36168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12112 KB |
Output is correct |
2 |
Correct |
3 ms |
12208 KB |
Output is correct |
3 |
Correct |
3 ms |
12112 KB |
Output is correct |
4 |
Correct |
3 ms |
12204 KB |
Output is correct |
5 |
Correct |
3 ms |
12112 KB |
Output is correct |
6 |
Correct |
3 ms |
12112 KB |
Output is correct |
7 |
Correct |
3 ms |
12112 KB |
Output is correct |
8 |
Correct |
3 ms |
12112 KB |
Output is correct |
9 |
Correct |
3 ms |
12112 KB |
Output is correct |
10 |
Correct |
3 ms |
12112 KB |
Output is correct |
11 |
Correct |
4 ms |
12112 KB |
Output is correct |
12 |
Correct |
3 ms |
12112 KB |
Output is correct |
13 |
Correct |
3 ms |
12112 KB |
Output is correct |
14 |
Correct |
3 ms |
12112 KB |
Output is correct |
15 |
Correct |
3 ms |
12112 KB |
Output is correct |
16 |
Correct |
3 ms |
12112 KB |
Output is correct |
17 |
Correct |
3 ms |
12112 KB |
Output is correct |
18 |
Correct |
3 ms |
12112 KB |
Output is correct |
19 |
Correct |
3 ms |
12112 KB |
Output is correct |
20 |
Correct |
3 ms |
12112 KB |
Output is correct |
21 |
Correct |
3 ms |
12112 KB |
Output is correct |
22 |
Correct |
3 ms |
12112 KB |
Output is correct |
23 |
Correct |
3 ms |
12112 KB |
Output is correct |
24 |
Correct |
4 ms |
12112 KB |
Output is correct |
25 |
Correct |
4 ms |
12112 KB |
Output is correct |
26 |
Correct |
3 ms |
12112 KB |
Output is correct |
27 |
Correct |
3 ms |
12112 KB |
Output is correct |
28 |
Correct |
3 ms |
12112 KB |
Output is correct |
29 |
Correct |
3 ms |
12112 KB |
Output is correct |
30 |
Correct |
3 ms |
12216 KB |
Output is correct |
31 |
Correct |
3 ms |
12112 KB |
Output is correct |
32 |
Correct |
3 ms |
12112 KB |
Output is correct |
33 |
Correct |
4 ms |
12112 KB |
Output is correct |
34 |
Correct |
3 ms |
12112 KB |
Output is correct |
35 |
Correct |
3 ms |
12368 KB |
Output is correct |
36 |
Correct |
3 ms |
12368 KB |
Output is correct |
37 |
Correct |
3 ms |
12112 KB |
Output is correct |
38 |
Correct |
3 ms |
12112 KB |
Output is correct |
39 |
Correct |
83 ms |
20304 KB |
Output is correct |
40 |
Correct |
81 ms |
19120 KB |
Output is correct |
41 |
Correct |
81 ms |
19036 KB |
Output is correct |
42 |
Correct |
79 ms |
20576 KB |
Output is correct |
43 |
Correct |
120 ms |
20836 KB |
Output is correct |
44 |
Correct |
84 ms |
20560 KB |
Output is correct |
45 |
Correct |
59 ms |
28204 KB |
Output is correct |
46 |
Correct |
42 ms |
35600 KB |
Output is correct |
47 |
Correct |
105 ms |
26940 KB |
Output is correct |
48 |
Correct |
186 ms |
66376 KB |
Output is correct |
49 |
Correct |
168 ms |
60756 KB |
Output is correct |
50 |
Correct |
104 ms |
27056 KB |
Output is correct |
51 |
Correct |
107 ms |
27168 KB |
Output is correct |
52 |
Correct |
123 ms |
27260 KB |
Output is correct |
53 |
Correct |
144 ms |
26184 KB |
Output is correct |
54 |
Correct |
252 ms |
36168 KB |
Output is correct |
55 |
Correct |
12 ms |
13136 KB |
Output is correct |
56 |
Correct |
9 ms |
12728 KB |
Output is correct |
57 |
Correct |
60 ms |
20560 KB |
Output is correct |
58 |
Correct |
36 ms |
20436 KB |
Output is correct |
59 |
Correct |
69 ms |
40264 KB |
Output is correct |
60 |
Correct |
160 ms |
64572 KB |
Output is correct |
61 |
Correct |
129 ms |
28488 KB |
Output is correct |
62 |
Correct |
99 ms |
27208 KB |
Output is correct |
63 |
Correct |
112 ms |
27208 KB |
Output is correct |
64 |
Correct |
364 ms |
31720 KB |
Output is correct |
65 |
Correct |
305 ms |
36936 KB |
Output is correct |
66 |
Correct |
160 ms |
58952 KB |
Output is correct |
67 |
Correct |
105 ms |
28088 KB |
Output is correct |
68 |
Correct |
212 ms |
35656 KB |
Output is correct |
69 |
Correct |
199 ms |
36032 KB |
Output is correct |
70 |
Correct |
190 ms |
34988 KB |
Output is correct |