#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef vector<int> vi;
typedef vector< vector <int> > vvi;
typedef pair<int, int> pii;
typedef pair < pair < int, int >, int > piii;
typedef pair < pair <int, int > , pair <int, int> > piiii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
#define FOR(i,a,b) for(int i = a; i < b; i ++)
#define RFOR(i,a,b) for(int i = a-1; i >= b; i --)
#define all(a) a.begin(), a.end()
#define endl '\n';
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
vector<vector<pair<int, ll>>> adj;
vector<ll> dep, dist;
vector<map<ll, ll>> paths;
ll N, K;
ll ans = 1e9;
void dfs(int u, int p, ll d, ll ds) {
dep[u] = d;
dist[u] = ds;
for(auto x : adj[u]) {
ll w = x.ss;
int v = x.ff;
if(v != p) {
dfs(v, u, d + 1, ds + w);
}
}
}
void dfs2(int u, int p, ll d, ll ds) {
ll tar = K + 2 * ds;
paths[u][ds] = d;
for(auto x : adj[u]) {
ll w = x.ss;
int v = x.ff;
if(v != p) {
dfs2(v, u, d + 1, ds + w);
if(paths[v].size() > paths[u].size()) {
swap(paths[u], paths[v]);
}
for(auto y : paths[v]) {
if(paths[u].count(tar - y.ff)) {
ans = min(ans, paths[u][tar-y.ff] + y.ss - 2 * d);
}
}
for(auto y : paths[v]) {
if(paths[u].count(y.ff)) {
paths[u][y.ff] = min(paths[u][y.ff], y.ss);
} else {
paths[u][y.ff] = y.ss;
}
}
}
}
if(paths[u].count(K + ds)) {
ans = min(ans, paths[u][K+ds] - d);
}
}
int best_path(int n, int k, int edges[][2], int weights[]) {
adj.resize(n);
dist.resize(n);
dep.resize(n);
paths.resize(n);
K = k;
FOR(i,0,n-1) {
ll u = edges[i][0];
ll v = edges[i][1];
ll w = weights[i];
adj[u].pb({v,w});
adj[v].pb({u,w});
}
dfs(0,0,0,0);
dfs2(0,0,0,0);
if(ans == 1e9) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
304 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
304 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
700 KB |
Output is correct |
26 |
Correct |
2 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
2 ms |
596 KB |
Output is correct |
30 |
Correct |
2 ms |
700 KB |
Output is correct |
31 |
Correct |
2 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
704 KB |
Output is correct |
33 |
Correct |
2 ms |
724 KB |
Output is correct |
34 |
Correct |
1 ms |
576 KB |
Output is correct |
35 |
Correct |
2 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
2 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
304 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
137 ms |
32052 KB |
Output is correct |
20 |
Correct |
138 ms |
32168 KB |
Output is correct |
21 |
Correct |
132 ms |
31868 KB |
Output is correct |
22 |
Correct |
132 ms |
31452 KB |
Output is correct |
23 |
Correct |
164 ms |
44340 KB |
Output is correct |
24 |
Correct |
129 ms |
34424 KB |
Output is correct |
25 |
Correct |
111 ms |
33684 KB |
Output is correct |
26 |
Correct |
60 ms |
42716 KB |
Output is correct |
27 |
Correct |
204 ms |
52532 KB |
Output is correct |
28 |
Correct |
335 ms |
98040 KB |
Output is correct |
29 |
Correct |
347 ms |
95820 KB |
Output is correct |
30 |
Correct |
215 ms |
52428 KB |
Output is correct |
31 |
Correct |
206 ms |
52548 KB |
Output is correct |
32 |
Correct |
295 ms |
52572 KB |
Output is correct |
33 |
Correct |
257 ms |
57464 KB |
Output is correct |
34 |
Correct |
350 ms |
89676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
304 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
700 KB |
Output is correct |
26 |
Correct |
2 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
2 ms |
596 KB |
Output is correct |
30 |
Correct |
2 ms |
700 KB |
Output is correct |
31 |
Correct |
2 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
704 KB |
Output is correct |
33 |
Correct |
2 ms |
724 KB |
Output is correct |
34 |
Correct |
1 ms |
576 KB |
Output is correct |
35 |
Correct |
2 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
2 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
137 ms |
32052 KB |
Output is correct |
40 |
Correct |
138 ms |
32168 KB |
Output is correct |
41 |
Correct |
132 ms |
31868 KB |
Output is correct |
42 |
Correct |
132 ms |
31452 KB |
Output is correct |
43 |
Correct |
164 ms |
44340 KB |
Output is correct |
44 |
Correct |
129 ms |
34424 KB |
Output is correct |
45 |
Correct |
111 ms |
33684 KB |
Output is correct |
46 |
Correct |
60 ms |
42716 KB |
Output is correct |
47 |
Correct |
204 ms |
52532 KB |
Output is correct |
48 |
Correct |
335 ms |
98040 KB |
Output is correct |
49 |
Correct |
347 ms |
95820 KB |
Output is correct |
50 |
Correct |
215 ms |
52428 KB |
Output is correct |
51 |
Correct |
206 ms |
52548 KB |
Output is correct |
52 |
Correct |
295 ms |
52572 KB |
Output is correct |
53 |
Correct |
257 ms |
57464 KB |
Output is correct |
54 |
Correct |
350 ms |
89676 KB |
Output is correct |
55 |
Correct |
15 ms |
4436 KB |
Output is correct |
56 |
Correct |
11 ms |
3132 KB |
Output is correct |
57 |
Correct |
75 ms |
29540 KB |
Output is correct |
58 |
Correct |
52 ms |
22076 KB |
Output is correct |
59 |
Correct |
111 ms |
48944 KB |
Output is correct |
60 |
Correct |
314 ms |
96456 KB |
Output is correct |
61 |
Correct |
238 ms |
55956 KB |
Output is correct |
62 |
Correct |
203 ms |
52484 KB |
Output is correct |
63 |
Correct |
281 ms |
52708 KB |
Output is correct |
64 |
Correct |
471 ms |
107384 KB |
Output is correct |
65 |
Correct |
450 ms |
105000 KB |
Output is correct |
66 |
Correct |
350 ms |
91328 KB |
Output is correct |
67 |
Correct |
172 ms |
45392 KB |
Output is correct |
68 |
Correct |
361 ms |
68440 KB |
Output is correct |
69 |
Correct |
407 ms |
73428 KB |
Output is correct |
70 |
Correct |
353 ms |
65680 KB |
Output is correct |