#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <unistd.h>
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#ifndef Local
#pragma GCC optimize("O3,unroll-loops")
#include "race.h"
#endif
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpi vector<pii>
#define vvpi vector<vpi>
#define vb vector<bool>
#define vvb vector<vb>
#define endl '\n'
#define sp << " " <<
#define F(i, s, n) for(int i = s; i < n; i++)
#define pb push_back
#define fi first
#define se second
int inf = 1e18;
const int N = 2e5+5;
int32_t l[N];
int32_t h[N][2];
vpi adj[N];
map<int, int> mp[N];
int k;
int ans = inf;
void dfs(int node, int par, int depth, int depl) {
mp[node][depl] = depth;
int totK = depl * 2 + k;
for(pii go : adj[node]) if(go.fi != par) {
dfs(go.fi, node, depth+1, depl+go.se);
if(mp[go.fi].size() > mp[node].size()) swap(mp[go.fi], mp[node]);
for(pii el : mp[go.fi]) {
if(mp[node].find(totK - el.fi) != mp[node].end()) {
ans = min(ans, mp[node][totK - el.fi] + el.se - depth*2);
}
}
for(pii el : mp[go.fi]) {
if(mp[node].find(el.fi) != mp[node].end()) mp[node][el.fi] = min(mp[node][el.fi], el.se);
else mp[node][el.fi] = el.se;
}
}
}
int32_t best_path(int32_t n, int32_t k, int32_t h[][2], int32_t l[]) {
F(i, 0, n-1) {
int u = h[i][0], v = h[i][1];
adj[u].pb({v, l[i]});
adj[v].pb({u, l[i]});
}
::k = k;
dfs(0, -1, 0, 0);
if(ans < 1e16) return ans;
else return -1;
}
#ifdef Local
void solve() {
int32_t n, k;
cin >> n >> k;
F(i, 0, n-1) cin >> h[i][0] >> h[i][1];
F(i, 0, n-1) cin >> l[i];
cout << best_path(n, k, h, l) << endl;
}
void setIO() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef Local
freopen("local.in", "r", stdin);
freopen("local.out", "w", stdout);
#else
// freopen("disrupt.in","r",stdin);
// freopen("disrupt.out","w",stdout);
#endif
}
signed main() {
setIO();
int t = 1;
//cin >> t;
while(t--) solve();
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18776 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
3 |
Correct |
3 ms |
18780 KB |
Output is correct |
4 |
Correct |
3 ms |
18780 KB |
Output is correct |
5 |
Correct |
3 ms |
18780 KB |
Output is correct |
6 |
Correct |
3 ms |
18780 KB |
Output is correct |
7 |
Correct |
4 ms |
18876 KB |
Output is correct |
8 |
Correct |
3 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18780 KB |
Output is correct |
11 |
Correct |
3 ms |
18780 KB |
Output is correct |
12 |
Correct |
5 ms |
18780 KB |
Output is correct |
13 |
Correct |
3 ms |
18780 KB |
Output is correct |
14 |
Correct |
4 ms |
18780 KB |
Output is correct |
15 |
Correct |
3 ms |
18780 KB |
Output is correct |
16 |
Correct |
3 ms |
18780 KB |
Output is correct |
17 |
Correct |
3 ms |
18776 KB |
Output is correct |
18 |
Correct |
3 ms |
18780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18776 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
3 |
Correct |
3 ms |
18780 KB |
Output is correct |
4 |
Correct |
3 ms |
18780 KB |
Output is correct |
5 |
Correct |
3 ms |
18780 KB |
Output is correct |
6 |
Correct |
3 ms |
18780 KB |
Output is correct |
7 |
Correct |
4 ms |
18876 KB |
Output is correct |
8 |
Correct |
3 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18780 KB |
Output is correct |
11 |
Correct |
3 ms |
18780 KB |
Output is correct |
12 |
Correct |
5 ms |
18780 KB |
Output is correct |
13 |
Correct |
3 ms |
18780 KB |
Output is correct |
14 |
Correct |
4 ms |
18780 KB |
Output is correct |
15 |
Correct |
3 ms |
18780 KB |
Output is correct |
16 |
Correct |
3 ms |
18780 KB |
Output is correct |
17 |
Correct |
3 ms |
18776 KB |
Output is correct |
18 |
Correct |
3 ms |
18780 KB |
Output is correct |
19 |
Correct |
3 ms |
18780 KB |
Output is correct |
20 |
Correct |
4 ms |
18780 KB |
Output is correct |
21 |
Correct |
4 ms |
19036 KB |
Output is correct |
22 |
Correct |
4 ms |
19036 KB |
Output is correct |
23 |
Correct |
6 ms |
19152 KB |
Output is correct |
24 |
Correct |
4 ms |
19036 KB |
Output is correct |
25 |
Correct |
5 ms |
19036 KB |
Output is correct |
26 |
Correct |
5 ms |
19036 KB |
Output is correct |
27 |
Correct |
4 ms |
19036 KB |
Output is correct |
28 |
Correct |
4 ms |
19036 KB |
Output is correct |
29 |
Correct |
4 ms |
19036 KB |
Output is correct |
30 |
Correct |
4 ms |
19036 KB |
Output is correct |
31 |
Correct |
4 ms |
19036 KB |
Output is correct |
32 |
Correct |
4 ms |
19036 KB |
Output is correct |
33 |
Correct |
5 ms |
19036 KB |
Output is correct |
34 |
Correct |
4 ms |
19036 KB |
Output is correct |
35 |
Correct |
4 ms |
19124 KB |
Output is correct |
36 |
Correct |
5 ms |
19036 KB |
Output is correct |
37 |
Correct |
4 ms |
19032 KB |
Output is correct |
38 |
Correct |
6 ms |
19036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18776 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
3 |
Correct |
3 ms |
18780 KB |
Output is correct |
4 |
Correct |
3 ms |
18780 KB |
Output is correct |
5 |
Correct |
3 ms |
18780 KB |
Output is correct |
6 |
Correct |
3 ms |
18780 KB |
Output is correct |
7 |
Correct |
4 ms |
18876 KB |
Output is correct |
8 |
Correct |
3 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18780 KB |
Output is correct |
11 |
Correct |
3 ms |
18780 KB |
Output is correct |
12 |
Correct |
5 ms |
18780 KB |
Output is correct |
13 |
Correct |
3 ms |
18780 KB |
Output is correct |
14 |
Correct |
4 ms |
18780 KB |
Output is correct |
15 |
Correct |
3 ms |
18780 KB |
Output is correct |
16 |
Correct |
3 ms |
18780 KB |
Output is correct |
17 |
Correct |
3 ms |
18776 KB |
Output is correct |
18 |
Correct |
3 ms |
18780 KB |
Output is correct |
19 |
Correct |
96 ms |
41808 KB |
Output is correct |
20 |
Correct |
108 ms |
41768 KB |
Output is correct |
21 |
Correct |
101 ms |
41504 KB |
Output is correct |
22 |
Correct |
94 ms |
40896 KB |
Output is correct |
23 |
Correct |
142 ms |
53868 KB |
Output is correct |
24 |
Correct |
112 ms |
43856 KB |
Output is correct |
25 |
Correct |
84 ms |
43432 KB |
Output is correct |
26 |
Correct |
55 ms |
52308 KB |
Output is correct |
27 |
Correct |
161 ms |
50924 KB |
Output is correct |
28 |
Correct |
260 ms |
96260 KB |
Output is correct |
29 |
Correct |
229 ms |
94032 KB |
Output is correct |
30 |
Correct |
153 ms |
50768 KB |
Output is correct |
31 |
Correct |
159 ms |
51096 KB |
Output is correct |
32 |
Correct |
200 ms |
50896 KB |
Output is correct |
33 |
Correct |
183 ms |
55892 KB |
Output is correct |
34 |
Correct |
297 ms |
87924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18776 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
3 |
Correct |
3 ms |
18780 KB |
Output is correct |
4 |
Correct |
3 ms |
18780 KB |
Output is correct |
5 |
Correct |
3 ms |
18780 KB |
Output is correct |
6 |
Correct |
3 ms |
18780 KB |
Output is correct |
7 |
Correct |
4 ms |
18876 KB |
Output is correct |
8 |
Correct |
3 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18780 KB |
Output is correct |
11 |
Correct |
3 ms |
18780 KB |
Output is correct |
12 |
Correct |
5 ms |
18780 KB |
Output is correct |
13 |
Correct |
3 ms |
18780 KB |
Output is correct |
14 |
Correct |
4 ms |
18780 KB |
Output is correct |
15 |
Correct |
3 ms |
18780 KB |
Output is correct |
16 |
Correct |
3 ms |
18780 KB |
Output is correct |
17 |
Correct |
3 ms |
18776 KB |
Output is correct |
18 |
Correct |
3 ms |
18780 KB |
Output is correct |
19 |
Correct |
3 ms |
18780 KB |
Output is correct |
20 |
Correct |
4 ms |
18780 KB |
Output is correct |
21 |
Correct |
4 ms |
19036 KB |
Output is correct |
22 |
Correct |
4 ms |
19036 KB |
Output is correct |
23 |
Correct |
6 ms |
19152 KB |
Output is correct |
24 |
Correct |
4 ms |
19036 KB |
Output is correct |
25 |
Correct |
5 ms |
19036 KB |
Output is correct |
26 |
Correct |
5 ms |
19036 KB |
Output is correct |
27 |
Correct |
4 ms |
19036 KB |
Output is correct |
28 |
Correct |
4 ms |
19036 KB |
Output is correct |
29 |
Correct |
4 ms |
19036 KB |
Output is correct |
30 |
Correct |
4 ms |
19036 KB |
Output is correct |
31 |
Correct |
4 ms |
19036 KB |
Output is correct |
32 |
Correct |
4 ms |
19036 KB |
Output is correct |
33 |
Correct |
5 ms |
19036 KB |
Output is correct |
34 |
Correct |
4 ms |
19036 KB |
Output is correct |
35 |
Correct |
4 ms |
19124 KB |
Output is correct |
36 |
Correct |
5 ms |
19036 KB |
Output is correct |
37 |
Correct |
4 ms |
19032 KB |
Output is correct |
38 |
Correct |
6 ms |
19036 KB |
Output is correct |
39 |
Correct |
96 ms |
41808 KB |
Output is correct |
40 |
Correct |
108 ms |
41768 KB |
Output is correct |
41 |
Correct |
101 ms |
41504 KB |
Output is correct |
42 |
Correct |
94 ms |
40896 KB |
Output is correct |
43 |
Correct |
142 ms |
53868 KB |
Output is correct |
44 |
Correct |
112 ms |
43856 KB |
Output is correct |
45 |
Correct |
84 ms |
43432 KB |
Output is correct |
46 |
Correct |
55 ms |
52308 KB |
Output is correct |
47 |
Correct |
161 ms |
50924 KB |
Output is correct |
48 |
Correct |
260 ms |
96260 KB |
Output is correct |
49 |
Correct |
229 ms |
94032 KB |
Output is correct |
50 |
Correct |
153 ms |
50768 KB |
Output is correct |
51 |
Correct |
159 ms |
51096 KB |
Output is correct |
52 |
Correct |
200 ms |
50896 KB |
Output is correct |
53 |
Correct |
183 ms |
55892 KB |
Output is correct |
54 |
Correct |
297 ms |
87924 KB |
Output is correct |
55 |
Correct |
15 ms |
21848 KB |
Output is correct |
56 |
Correct |
12 ms |
20976 KB |
Output is correct |
57 |
Correct |
58 ms |
39248 KB |
Output is correct |
58 |
Correct |
44 ms |
31940 KB |
Output is correct |
59 |
Correct |
74 ms |
58580 KB |
Output is correct |
60 |
Correct |
217 ms |
94696 KB |
Output is correct |
61 |
Correct |
190 ms |
54356 KB |
Output is correct |
62 |
Correct |
153 ms |
50892 KB |
Output is correct |
63 |
Correct |
220 ms |
51028 KB |
Output is correct |
64 |
Correct |
386 ms |
105356 KB |
Output is correct |
65 |
Correct |
370 ms |
102932 KB |
Output is correct |
66 |
Correct |
239 ms |
89172 KB |
Output is correct |
67 |
Correct |
167 ms |
43244 KB |
Output is correct |
68 |
Correct |
283 ms |
66692 KB |
Output is correct |
69 |
Correct |
317 ms |
71468 KB |
Output is correct |
70 |
Correct |
309 ms |
64636 KB |
Output is correct |