#include "race.h"
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define debug(x) cerr << #x << " = " << (x) << ' '
#define bug(x) cerr << (x) << ' '
#define endl cerr << '\n'
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)(v).size()
#define lowbit(x) (x)&-(x)
#define pb emplace_back
#define F first
#define S second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
//#define TEST
const int N = 2e5+5;
const int INF = 2e9;
ll K, sum[N], dep[N], ans = INF;
vector<pll> g[N];
inline void dfs0(int x, int pre)
{
for (pll p : g[x])
{
int v = p.F, w = p.S;
if (v == pre) continue;
sum[v] = sum[x] + w, dep[v] = dep[x] + 1;
dfs0(v, x);
}
}
inline void push(map<ll, int> &mp, pll p)
{
if (mp.count(p.F)) mp[p.F] = min(mp[p.F], (int)p.S);
else mp[p.F] = p.S;
}
inline map<ll, int> dfs(int x, int pre)
{
map<ll, int> mp, tmp;
for (pll e : g[x])
{
int v = e.F;
if (v == pre) continue;
tmp = dfs(v, x);
if (SZ(tmp) > SZ(mp)) swap(mp, tmp);
for (auto p : tmp)
{
if (mp.count(K-p.F + sum[x]*2))
ans = min(ans, p.S + mp[K-p.F + sum[x]*2] - dep[x]*2);
}
for (auto p : tmp) push(mp, p);
}
if (mp.count(K + sum[x]))
ans = min(ans, mp[K + sum[x]] - dep[x]);
push(mp, pll{sum[x], dep[x]});
//debug(x), endl;
//for (auto p : mp) debug(p.F), debug(p.S), endl; endl;
return mp;
}
int best_path(int n, int _k, int e[][2], int len[])
{
K = _k;
for (int i=0; i < n-1; ++i)
{
int x = e[i][0], y = e[i][1], w = len[i];
++x, ++y;
g[x].pb(y, w), g[y].pb(x, w);
}
dfs0(1, 0);
dfs(1, 0);
ans = (ans == INF)?-1 : ans;
return ans;
}
#ifdef TEST
int main(void)
{ IO
int n, i;
cin >> n >> K;
for (i=0; i < n-1; ++i)
{
int x, y, w; cin >> x >> y >> w;
++x, ++y;
g[x].pb(y, w), g[y].pb(x, w);
}
dfs0(1, 0);
dfs(1, 0);
//for (i=1; i <= n; ++i) bug(sum[i]); endl;
//for (i=1; i <= n; ++i) bug(dep[i]); endl;
//debug(ans), endl;
if (ans == INF) cout << "-1\n";
else cout << ans << '\n';
return 0;
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10680 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10680 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10844 KB |
Output is correct |
24 |
Correct |
3 ms |
10588 KB |
Output is correct |
25 |
Correct |
3 ms |
10844 KB |
Output is correct |
26 |
Correct |
3 ms |
10844 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
3 ms |
10700 KB |
Output is correct |
29 |
Correct |
3 ms |
10588 KB |
Output is correct |
30 |
Correct |
3 ms |
10840 KB |
Output is correct |
31 |
Correct |
3 ms |
10588 KB |
Output is correct |
32 |
Correct |
3 ms |
10588 KB |
Output is correct |
33 |
Correct |
3 ms |
10844 KB |
Output is correct |
34 |
Correct |
2 ms |
10844 KB |
Output is correct |
35 |
Correct |
2 ms |
10844 KB |
Output is correct |
36 |
Correct |
2 ms |
10844 KB |
Output is correct |
37 |
Correct |
2 ms |
10844 KB |
Output is correct |
38 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10680 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
68 ms |
18672 KB |
Output is correct |
20 |
Correct |
67 ms |
19796 KB |
Output is correct |
21 |
Correct |
62 ms |
19204 KB |
Output is correct |
22 |
Correct |
67 ms |
19804 KB |
Output is correct |
23 |
Correct |
88 ms |
20484 KB |
Output is correct |
24 |
Correct |
71 ms |
19484 KB |
Output is correct |
25 |
Correct |
71 ms |
34632 KB |
Output is correct |
26 |
Correct |
52 ms |
47008 KB |
Output is correct |
27 |
Correct |
125 ms |
26840 KB |
Output is correct |
28 |
Correct |
217 ms |
93652 KB |
Output is correct |
29 |
Correct |
201 ms |
91396 KB |
Output is correct |
30 |
Correct |
123 ms |
27324 KB |
Output is correct |
31 |
Correct |
123 ms |
27464 KB |
Output is correct |
32 |
Correct |
142 ms |
27440 KB |
Output is correct |
33 |
Correct |
130 ms |
26912 KB |
Output is correct |
34 |
Correct |
237 ms |
39824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10680 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10844 KB |
Output is correct |
24 |
Correct |
3 ms |
10588 KB |
Output is correct |
25 |
Correct |
3 ms |
10844 KB |
Output is correct |
26 |
Correct |
3 ms |
10844 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
3 ms |
10700 KB |
Output is correct |
29 |
Correct |
3 ms |
10588 KB |
Output is correct |
30 |
Correct |
3 ms |
10840 KB |
Output is correct |
31 |
Correct |
3 ms |
10588 KB |
Output is correct |
32 |
Correct |
3 ms |
10588 KB |
Output is correct |
33 |
Correct |
3 ms |
10844 KB |
Output is correct |
34 |
Correct |
2 ms |
10844 KB |
Output is correct |
35 |
Correct |
2 ms |
10844 KB |
Output is correct |
36 |
Correct |
2 ms |
10844 KB |
Output is correct |
37 |
Correct |
2 ms |
10844 KB |
Output is correct |
38 |
Correct |
2 ms |
10844 KB |
Output is correct |
39 |
Correct |
68 ms |
18672 KB |
Output is correct |
40 |
Correct |
67 ms |
19796 KB |
Output is correct |
41 |
Correct |
62 ms |
19204 KB |
Output is correct |
42 |
Correct |
67 ms |
19804 KB |
Output is correct |
43 |
Correct |
88 ms |
20484 KB |
Output is correct |
44 |
Correct |
71 ms |
19484 KB |
Output is correct |
45 |
Correct |
71 ms |
34632 KB |
Output is correct |
46 |
Correct |
52 ms |
47008 KB |
Output is correct |
47 |
Correct |
125 ms |
26840 KB |
Output is correct |
48 |
Correct |
217 ms |
93652 KB |
Output is correct |
49 |
Correct |
201 ms |
91396 KB |
Output is correct |
50 |
Correct |
123 ms |
27324 KB |
Output is correct |
51 |
Correct |
123 ms |
27464 KB |
Output is correct |
52 |
Correct |
142 ms |
27440 KB |
Output is correct |
53 |
Correct |
130 ms |
26912 KB |
Output is correct |
54 |
Correct |
237 ms |
39824 KB |
Output is correct |
55 |
Correct |
12 ms |
11608 KB |
Output is correct |
56 |
Correct |
6 ms |
11100 KB |
Output is correct |
57 |
Correct |
44 ms |
19036 KB |
Output is correct |
58 |
Correct |
38 ms |
18892 KB |
Output is correct |
59 |
Correct |
95 ms |
53184 KB |
Output is correct |
60 |
Correct |
215 ms |
91988 KB |
Output is correct |
61 |
Correct |
169 ms |
28240 KB |
Output is correct |
62 |
Correct |
121 ms |
27200 KB |
Output is correct |
63 |
Correct |
142 ms |
27216 KB |
Output is correct |
64 |
Correct |
335 ms |
33940 KB |
Output is correct |
65 |
Correct |
311 ms |
40272 KB |
Output is correct |
66 |
Correct |
227 ms |
81748 KB |
Output is correct |
67 |
Correct |
94 ms |
27328 KB |
Output is correct |
68 |
Correct |
224 ms |
37480 KB |
Output is correct |
69 |
Correct |
242 ms |
39508 KB |
Output is correct |
70 |
Correct |
219 ms |
36996 KB |
Output is correct |