#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
constexpr ll maxN = 2e5+10;
constexpr ll inf = 1e15;
vector<pair<ll, ll>> adj[maxN];
ll res = inf, k;
map<ll, ll> dfs(ll v, ll p, ll konst, ll dep) {
map<ll, ll> mp;
mp[konst]=dep;
for(auto i : adj[v]) {
if(i.first==p)continue;
map<ll, ll> tmp = dfs(i.first, v, i.second+konst, dep+1);
if(tmp.size()>mp.size())swap(mp, tmp);
for(auto x : tmp) {
ll val = x.first;
ll dep1 = x.second;
ll needed = k+2*konst-val;
if(mp.count(needed)) {
ll dep2 = mp[needed];
res=min(res, dep1+dep2-2*dep);
}
}
for(auto x : tmp) {
if(!mp.count(x.first))mp[x.first]=x.second;
else mp[x.first]=min(x.second, mp[x.first]);
}
}
return mp;
}
int best_path(int N, int K, int H[][2], int L[])
{
k=K;
for(int i = 0; i < N-1; i++) {
ll aa = H[i][0], bb = H[i][1];
adj[aa].push_back({bb, L[i]});
adj[bb].push_back({aa, L[i]});
}
dfs(0, -1, 0, 0);
if(res==inf) {
return -1;
}
return res;
}
/*int nn, kk;
int H[maxN][2];
int L[maxN];
int solution;
int main()
{
cin>>nn>>kk;
for(int i = 0; i < nn-1; i++) {
cin>>H[i][0]>>H[i][1];
}
for(int i = 0; i < nn-1; i++) {
cin>>L[i];
}
cout<<best_path(nn,kk,H,L);
return 0;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
4 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
6736 KB |
Output is correct |
8 |
Correct |
2 ms |
6576 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4980 KB |
Output is correct |
12 |
Correct |
2 ms |
4952 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4952 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
5080 KB |
Output is correct |
18 |
Correct |
2 ms |
4988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
4 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
6736 KB |
Output is correct |
8 |
Correct |
2 ms |
6576 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4980 KB |
Output is correct |
12 |
Correct |
2 ms |
4952 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4952 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
5080 KB |
Output is correct |
18 |
Correct |
2 ms |
4988 KB |
Output is correct |
19 |
Correct |
2 ms |
6492 KB |
Output is correct |
20 |
Correct |
3 ms |
4956 KB |
Output is correct |
21 |
Correct |
3 ms |
5212 KB |
Output is correct |
22 |
Correct |
3 ms |
5212 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
5212 KB |
Output is correct |
25 |
Correct |
3 ms |
5136 KB |
Output is correct |
26 |
Correct |
3 ms |
5212 KB |
Output is correct |
27 |
Correct |
4 ms |
6748 KB |
Output is correct |
28 |
Correct |
3 ms |
6744 KB |
Output is correct |
29 |
Correct |
3 ms |
5120 KB |
Output is correct |
30 |
Correct |
3 ms |
5212 KB |
Output is correct |
31 |
Correct |
4 ms |
5212 KB |
Output is correct |
32 |
Correct |
3 ms |
5212 KB |
Output is correct |
33 |
Correct |
3 ms |
5212 KB |
Output is correct |
34 |
Correct |
3 ms |
5212 KB |
Output is correct |
35 |
Correct |
3 ms |
5212 KB |
Output is correct |
36 |
Correct |
3 ms |
5212 KB |
Output is correct |
37 |
Correct |
3 ms |
5308 KB |
Output is correct |
38 |
Correct |
3 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
4 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
6736 KB |
Output is correct |
8 |
Correct |
2 ms |
6576 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4980 KB |
Output is correct |
12 |
Correct |
2 ms |
4952 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4952 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
5080 KB |
Output is correct |
18 |
Correct |
2 ms |
4988 KB |
Output is correct |
19 |
Correct |
60 ms |
11404 KB |
Output is correct |
20 |
Correct |
56 ms |
12880 KB |
Output is correct |
21 |
Correct |
53 ms |
13068 KB |
Output is correct |
22 |
Correct |
67 ms |
13084 KB |
Output is correct |
23 |
Correct |
83 ms |
13616 KB |
Output is correct |
24 |
Correct |
63 ms |
12636 KB |
Output is correct |
25 |
Correct |
48 ms |
28496 KB |
Output is correct |
26 |
Correct |
54 ms |
42324 KB |
Output is correct |
27 |
Correct |
111 ms |
22876 KB |
Output is correct |
28 |
Correct |
190 ms |
80744 KB |
Output is correct |
29 |
Correct |
175 ms |
78160 KB |
Output is correct |
30 |
Correct |
107 ms |
23040 KB |
Output is correct |
31 |
Correct |
104 ms |
22864 KB |
Output is correct |
32 |
Correct |
107 ms |
23108 KB |
Output is correct |
33 |
Correct |
120 ms |
22356 KB |
Output is correct |
34 |
Correct |
206 ms |
35920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
4 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
6736 KB |
Output is correct |
8 |
Correct |
2 ms |
6576 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4980 KB |
Output is correct |
12 |
Correct |
2 ms |
4952 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4952 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
5080 KB |
Output is correct |
18 |
Correct |
2 ms |
4988 KB |
Output is correct |
19 |
Correct |
2 ms |
6492 KB |
Output is correct |
20 |
Correct |
3 ms |
4956 KB |
Output is correct |
21 |
Correct |
3 ms |
5212 KB |
Output is correct |
22 |
Correct |
3 ms |
5212 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
5212 KB |
Output is correct |
25 |
Correct |
3 ms |
5136 KB |
Output is correct |
26 |
Correct |
3 ms |
5212 KB |
Output is correct |
27 |
Correct |
4 ms |
6748 KB |
Output is correct |
28 |
Correct |
3 ms |
6744 KB |
Output is correct |
29 |
Correct |
3 ms |
5120 KB |
Output is correct |
30 |
Correct |
3 ms |
5212 KB |
Output is correct |
31 |
Correct |
4 ms |
5212 KB |
Output is correct |
32 |
Correct |
3 ms |
5212 KB |
Output is correct |
33 |
Correct |
3 ms |
5212 KB |
Output is correct |
34 |
Correct |
3 ms |
5212 KB |
Output is correct |
35 |
Correct |
3 ms |
5212 KB |
Output is correct |
36 |
Correct |
3 ms |
5212 KB |
Output is correct |
37 |
Correct |
3 ms |
5308 KB |
Output is correct |
38 |
Correct |
3 ms |
5212 KB |
Output is correct |
39 |
Correct |
60 ms |
11404 KB |
Output is correct |
40 |
Correct |
56 ms |
12880 KB |
Output is correct |
41 |
Correct |
53 ms |
13068 KB |
Output is correct |
42 |
Correct |
67 ms |
13084 KB |
Output is correct |
43 |
Correct |
83 ms |
13616 KB |
Output is correct |
44 |
Correct |
63 ms |
12636 KB |
Output is correct |
45 |
Correct |
48 ms |
28496 KB |
Output is correct |
46 |
Correct |
54 ms |
42324 KB |
Output is correct |
47 |
Correct |
111 ms |
22876 KB |
Output is correct |
48 |
Correct |
190 ms |
80744 KB |
Output is correct |
49 |
Correct |
175 ms |
78160 KB |
Output is correct |
50 |
Correct |
107 ms |
23040 KB |
Output is correct |
51 |
Correct |
104 ms |
22864 KB |
Output is correct |
52 |
Correct |
107 ms |
23108 KB |
Output is correct |
53 |
Correct |
120 ms |
22356 KB |
Output is correct |
54 |
Correct |
206 ms |
35920 KB |
Output is correct |
55 |
Correct |
12 ms |
7768 KB |
Output is correct |
56 |
Correct |
6 ms |
7500 KB |
Output is correct |
57 |
Correct |
35 ms |
12884 KB |
Output is correct |
58 |
Correct |
31 ms |
11968 KB |
Output is correct |
59 |
Correct |
69 ms |
43604 KB |
Output is correct |
60 |
Correct |
173 ms |
78928 KB |
Output is correct |
61 |
Correct |
132 ms |
24656 KB |
Output is correct |
62 |
Correct |
101 ms |
23084 KB |
Output is correct |
63 |
Correct |
119 ms |
23124 KB |
Output is correct |
64 |
Correct |
315 ms |
31316 KB |
Output is correct |
65 |
Correct |
278 ms |
35668 KB |
Output is correct |
66 |
Correct |
189 ms |
72640 KB |
Output is correct |
67 |
Correct |
76 ms |
21888 KB |
Output is correct |
68 |
Correct |
203 ms |
33600 KB |
Output is correct |
69 |
Correct |
228 ms |
38132 KB |
Output is correct |
70 |
Correct |
180 ms |
32336 KB |
Output is correct |