#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) {
for(pii go : adj[node]) if(go.fi != par) dfs(go.fi, node, depth+1, depl+go.se);
mp[node][depl] = depth;
for(pii go : adj[node]) if(go.fi != par) {
if(mp[go.fi].size() > mp[node].size()) swap(mp[go.fi], mp[node]);
for(pii el : mp[go.fi]) {
int remK = k - (el.fi - depl);
if(mp[node].find(remK + depl) != mp[node].end()) {
ans = min(ans, mp[node][remK + depl] + 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 |
4 ms |
18780 KB |
Output is correct |
2 |
Correct |
3 ms |
18876 KB |
Output is correct |
3 |
Correct |
4 ms |
18780 KB |
Output is correct |
4 |
Correct |
4 ms |
18780 KB |
Output is correct |
5 |
Correct |
4 ms |
18776 KB |
Output is correct |
6 |
Correct |
4 ms |
18780 KB |
Output is correct |
7 |
Correct |
3 ms |
18780 KB |
Output is correct |
8 |
Correct |
4 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
3 ms |
18780 KB |
Output is correct |
11 |
Correct |
3 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18776 KB |
Output is correct |
13 |
Correct |
3 ms |
18780 KB |
Output is correct |
14 |
Correct |
5 ms |
18780 KB |
Output is correct |
15 |
Correct |
4 ms |
18780 KB |
Output is correct |
16 |
Correct |
4 ms |
18780 KB |
Output is correct |
17 |
Correct |
4 ms |
18776 KB |
Output is correct |
18 |
Correct |
4 ms |
18780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18780 KB |
Output is correct |
2 |
Correct |
3 ms |
18876 KB |
Output is correct |
3 |
Correct |
4 ms |
18780 KB |
Output is correct |
4 |
Correct |
4 ms |
18780 KB |
Output is correct |
5 |
Correct |
4 ms |
18776 KB |
Output is correct |
6 |
Correct |
4 ms |
18780 KB |
Output is correct |
7 |
Correct |
3 ms |
18780 KB |
Output is correct |
8 |
Correct |
4 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
3 ms |
18780 KB |
Output is correct |
11 |
Correct |
3 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18776 KB |
Output is correct |
13 |
Correct |
3 ms |
18780 KB |
Output is correct |
14 |
Correct |
5 ms |
18780 KB |
Output is correct |
15 |
Correct |
4 ms |
18780 KB |
Output is correct |
16 |
Correct |
4 ms |
18780 KB |
Output is correct |
17 |
Correct |
4 ms |
18776 KB |
Output is correct |
18 |
Correct |
4 ms |
18780 KB |
Output is correct |
19 |
Correct |
3 ms |
18780 KB |
Output is correct |
20 |
Correct |
3 ms |
18780 KB |
Output is correct |
21 |
Correct |
4 ms |
19140 KB |
Output is correct |
22 |
Correct |
4 ms |
19036 KB |
Output is correct |
23 |
Correct |
5 ms |
19036 KB |
Output is correct |
24 |
Correct |
4 ms |
19036 KB |
Output is correct |
25 |
Correct |
4 ms |
19036 KB |
Output is correct |
26 |
Correct |
5 ms |
19036 KB |
Output is correct |
27 |
Correct |
5 ms |
18880 KB |
Output is correct |
28 |
Correct |
5 ms |
19032 KB |
Output is correct |
29 |
Correct |
5 ms |
19036 KB |
Output is correct |
30 |
Correct |
5 ms |
19032 KB |
Output is correct |
31 |
Correct |
5 ms |
18968 KB |
Output is correct |
32 |
Correct |
4 ms |
19036 KB |
Output is correct |
33 |
Correct |
4 ms |
19036 KB |
Output is correct |
34 |
Correct |
4 ms |
19032 KB |
Output is correct |
35 |
Correct |
4 ms |
19036 KB |
Output is correct |
36 |
Correct |
4 ms |
19036 KB |
Output is correct |
37 |
Correct |
4 ms |
19036 KB |
Output is correct |
38 |
Correct |
5 ms |
19036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18780 KB |
Output is correct |
2 |
Correct |
3 ms |
18876 KB |
Output is correct |
3 |
Correct |
4 ms |
18780 KB |
Output is correct |
4 |
Correct |
4 ms |
18780 KB |
Output is correct |
5 |
Correct |
4 ms |
18776 KB |
Output is correct |
6 |
Correct |
4 ms |
18780 KB |
Output is correct |
7 |
Correct |
3 ms |
18780 KB |
Output is correct |
8 |
Correct |
4 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
3 ms |
18780 KB |
Output is correct |
11 |
Correct |
3 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18776 KB |
Output is correct |
13 |
Correct |
3 ms |
18780 KB |
Output is correct |
14 |
Correct |
5 ms |
18780 KB |
Output is correct |
15 |
Correct |
4 ms |
18780 KB |
Output is correct |
16 |
Correct |
4 ms |
18780 KB |
Output is correct |
17 |
Correct |
4 ms |
18776 KB |
Output is correct |
18 |
Correct |
4 ms |
18780 KB |
Output is correct |
19 |
Correct |
90 ms |
41644 KB |
Output is correct |
20 |
Correct |
101 ms |
41580 KB |
Output is correct |
21 |
Correct |
92 ms |
41556 KB |
Output is correct |
22 |
Correct |
89 ms |
41040 KB |
Output is correct |
23 |
Correct |
144 ms |
53844 KB |
Output is correct |
24 |
Correct |
97 ms |
43868 KB |
Output is correct |
25 |
Correct |
78 ms |
42472 KB |
Output is correct |
26 |
Correct |
54 ms |
50776 KB |
Output is correct |
27 |
Correct |
170 ms |
50980 KB |
Output is correct |
28 |
Correct |
228 ms |
92752 KB |
Output is correct |
29 |
Correct |
258 ms |
90964 KB |
Output is correct |
30 |
Correct |
154 ms |
51232 KB |
Output is correct |
31 |
Correct |
165 ms |
50768 KB |
Output is correct |
32 |
Correct |
214 ms |
51040 KB |
Output is correct |
33 |
Correct |
197 ms |
55688 KB |
Output is correct |
34 |
Correct |
281 ms |
87632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18780 KB |
Output is correct |
2 |
Correct |
3 ms |
18876 KB |
Output is correct |
3 |
Correct |
4 ms |
18780 KB |
Output is correct |
4 |
Correct |
4 ms |
18780 KB |
Output is correct |
5 |
Correct |
4 ms |
18776 KB |
Output is correct |
6 |
Correct |
4 ms |
18780 KB |
Output is correct |
7 |
Correct |
3 ms |
18780 KB |
Output is correct |
8 |
Correct |
4 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
3 ms |
18780 KB |
Output is correct |
11 |
Correct |
3 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18776 KB |
Output is correct |
13 |
Correct |
3 ms |
18780 KB |
Output is correct |
14 |
Correct |
5 ms |
18780 KB |
Output is correct |
15 |
Correct |
4 ms |
18780 KB |
Output is correct |
16 |
Correct |
4 ms |
18780 KB |
Output is correct |
17 |
Correct |
4 ms |
18776 KB |
Output is correct |
18 |
Correct |
4 ms |
18780 KB |
Output is correct |
19 |
Correct |
3 ms |
18780 KB |
Output is correct |
20 |
Correct |
3 ms |
18780 KB |
Output is correct |
21 |
Correct |
4 ms |
19140 KB |
Output is correct |
22 |
Correct |
4 ms |
19036 KB |
Output is correct |
23 |
Correct |
5 ms |
19036 KB |
Output is correct |
24 |
Correct |
4 ms |
19036 KB |
Output is correct |
25 |
Correct |
4 ms |
19036 KB |
Output is correct |
26 |
Correct |
5 ms |
19036 KB |
Output is correct |
27 |
Correct |
5 ms |
18880 KB |
Output is correct |
28 |
Correct |
5 ms |
19032 KB |
Output is correct |
29 |
Correct |
5 ms |
19036 KB |
Output is correct |
30 |
Correct |
5 ms |
19032 KB |
Output is correct |
31 |
Correct |
5 ms |
18968 KB |
Output is correct |
32 |
Correct |
4 ms |
19036 KB |
Output is correct |
33 |
Correct |
4 ms |
19036 KB |
Output is correct |
34 |
Correct |
4 ms |
19032 KB |
Output is correct |
35 |
Correct |
4 ms |
19036 KB |
Output is correct |
36 |
Correct |
4 ms |
19036 KB |
Output is correct |
37 |
Correct |
4 ms |
19036 KB |
Output is correct |
38 |
Correct |
5 ms |
19036 KB |
Output is correct |
39 |
Correct |
90 ms |
41644 KB |
Output is correct |
40 |
Correct |
101 ms |
41580 KB |
Output is correct |
41 |
Correct |
92 ms |
41556 KB |
Output is correct |
42 |
Correct |
89 ms |
41040 KB |
Output is correct |
43 |
Correct |
144 ms |
53844 KB |
Output is correct |
44 |
Correct |
97 ms |
43868 KB |
Output is correct |
45 |
Correct |
78 ms |
42472 KB |
Output is correct |
46 |
Correct |
54 ms |
50776 KB |
Output is correct |
47 |
Correct |
170 ms |
50980 KB |
Output is correct |
48 |
Correct |
228 ms |
92752 KB |
Output is correct |
49 |
Correct |
258 ms |
90964 KB |
Output is correct |
50 |
Correct |
154 ms |
51232 KB |
Output is correct |
51 |
Correct |
165 ms |
50768 KB |
Output is correct |
52 |
Correct |
214 ms |
51040 KB |
Output is correct |
53 |
Correct |
197 ms |
55688 KB |
Output is correct |
54 |
Correct |
281 ms |
87632 KB |
Output is correct |
55 |
Correct |
18 ms |
21848 KB |
Output is correct |
56 |
Correct |
13 ms |
20828 KB |
Output is correct |
57 |
Correct |
58 ms |
39252 KB |
Output is correct |
58 |
Correct |
44 ms |
31936 KB |
Output is correct |
59 |
Correct |
81 ms |
56912 KB |
Output is correct |
60 |
Correct |
220 ms |
91732 KB |
Output is correct |
61 |
Correct |
208 ms |
54144 KB |
Output is correct |
62 |
Correct |
151 ms |
50780 KB |
Output is correct |
63 |
Correct |
209 ms |
51024 KB |
Output is correct |
64 |
Correct |
373 ms |
105368 KB |
Output is correct |
65 |
Correct |
360 ms |
103000 KB |
Output is correct |
66 |
Correct |
254 ms |
86784 KB |
Output is correct |
67 |
Correct |
130 ms |
43188 KB |
Output is correct |
68 |
Correct |
285 ms |
66640 KB |
Output is correct |
69 |
Correct |
306 ms |
71216 KB |
Output is correct |
70 |
Correct |
279 ms |
64640 KB |
Output is correct |