#include "race.h"
#include <bits/stdc++.h>
#define M_PI 3.14159265358979323846
#define FILER 0
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll MOD = pow(10, 9) + 7;
const ll INFL = 0x3f3f3f3f3f3f3f3f;
const ull INFUL = 0x3f3f3f3f3f3f3f3f;
const ll INFT = 0x3f3f3f3f;
const ull MAX = 1LL << 24;
const ll MODD = 998244353;
const double EPS = 1e-10;
#define V vector
#define pll pair<ll, ll>
#define pull2 pair<ull,ull>
#define MS multiset
#define M map
#define Q queue
#define PQ priority_queue
#define IOF ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define FOR(typ,i,a,b,c) for(typ i = a; i < b; i += c)
#define FORR(typ,i,a,b,c) for(typ i = a; i > b; i -= c)
#define FORA(a,i) for(auto &i : a)
#define all(v) v.begin(),v.end()
#define sz(v) v.size()
#define sorta(a) sort(all(a))
#define sortd(a) sort(all(a), greater<ll>())
#define setp(x) setprecision(x)<<fixed
#define RET return
#define log(a,b) log(b)/log(a)
#define WH(s) while(s)
#define WHI(t) WH(t--)
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define Yes cout<<"Yes"<<endl;
#define No cout<<"No"<<endl;
#define YESNO(s) cout << (s ? "YES" : "NO") << endl;
#define YesNo(s) cout<<(s?"Yes":"No")<<endl;
#define TYP 0
using namespace std;
V<V<pll>> g;
V<map<ll, ll>> ds;
V<ll> d, s;
ll res = INFL;
void dfs(ll u, ll p, ll dep, ll su)
{
ds[u][su] = dep;
d[u] = dep;
s[u] = su;
FORA(g[u], v)
{
if (v.first == p)continue;
dfs(v.first, u, dep + 1, su + v.second);
}
}
void calc(ll u, ll p, ll k)
{
FORA(g[u], v)
{
if (v.first == p)continue;
calc(v.first, u, k);
if (ds[u].size() < ds[v.first].size())swap(ds[u], ds[v.first]);
FORA(ds[v.first], dsv)
{
auto it = ds[u].find(k + 2*s[u] - dsv.first);
if ( it != ds[u].end())res = min(res, it->second + dsv.second - 2 * d[u]);
}
FORA(ds[v.first], dsv)
{
if (ds[u].find(dsv.first) == ds[u].end())ds[u].insert(dsv);
else ds[u][dsv.first] = min(ds[u][dsv.first], dsv.second);
}
}
}
int best_path(int n, int k , int h[][2], int l[])
{
g.resize(n + 1); ds.resize(n + 1); d.resize(n + 1); s.resize(n + 1);
FOR(ll, i, 0, n-1, 1)g[h[i][0]].push_back({ h[i][1], l[i] }), g[h[i][1]].push_back({ h[i][0], l[i] });
dfs(0, -1, 0, 0);
calc(0, 0, k);
RET res == INFL ? -1 : res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 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 |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
388 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
752 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
1 ms |
532 KB |
Output is correct |
38 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 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 |
121 ms |
30800 KB |
Output is correct |
20 |
Correct |
147 ms |
30776 KB |
Output is correct |
21 |
Correct |
134 ms |
30448 KB |
Output is correct |
22 |
Correct |
141 ms |
31428 KB |
Output is correct |
23 |
Correct |
144 ms |
44316 KB |
Output is correct |
24 |
Correct |
123 ms |
34380 KB |
Output is correct |
25 |
Correct |
91 ms |
31284 KB |
Output is correct |
26 |
Correct |
51 ms |
37980 KB |
Output is correct |
27 |
Correct |
211 ms |
52512 KB |
Output is correct |
28 |
Correct |
280 ms |
87568 KB |
Output is correct |
29 |
Correct |
318 ms |
86136 KB |
Output is correct |
30 |
Correct |
188 ms |
51620 KB |
Output is correct |
31 |
Correct |
189 ms |
51656 KB |
Output is correct |
32 |
Correct |
263 ms |
51612 KB |
Output is correct |
33 |
Correct |
258 ms |
56532 KB |
Output is correct |
34 |
Correct |
318 ms |
88380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 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 |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
388 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
752 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
1 ms |
532 KB |
Output is correct |
38 |
Correct |
2 ms |
596 KB |
Output is correct |
39 |
Correct |
121 ms |
30800 KB |
Output is correct |
40 |
Correct |
147 ms |
30776 KB |
Output is correct |
41 |
Correct |
134 ms |
30448 KB |
Output is correct |
42 |
Correct |
141 ms |
31428 KB |
Output is correct |
43 |
Correct |
144 ms |
44316 KB |
Output is correct |
44 |
Correct |
123 ms |
34380 KB |
Output is correct |
45 |
Correct |
91 ms |
31284 KB |
Output is correct |
46 |
Correct |
51 ms |
37980 KB |
Output is correct |
47 |
Correct |
211 ms |
52512 KB |
Output is correct |
48 |
Correct |
280 ms |
87568 KB |
Output is correct |
49 |
Correct |
318 ms |
86136 KB |
Output is correct |
50 |
Correct |
188 ms |
51620 KB |
Output is correct |
51 |
Correct |
189 ms |
51656 KB |
Output is correct |
52 |
Correct |
263 ms |
51612 KB |
Output is correct |
53 |
Correct |
258 ms |
56532 KB |
Output is correct |
54 |
Correct |
318 ms |
88380 KB |
Output is correct |
55 |
Correct |
14 ms |
4396 KB |
Output is correct |
56 |
Correct |
6 ms |
3136 KB |
Output is correct |
57 |
Correct |
63 ms |
29612 KB |
Output is correct |
58 |
Correct |
47 ms |
22124 KB |
Output is correct |
59 |
Correct |
80 ms |
44212 KB |
Output is correct |
60 |
Correct |
254 ms |
86748 KB |
Output is correct |
61 |
Correct |
252 ms |
54996 KB |
Output is correct |
62 |
Correct |
179 ms |
51512 KB |
Output is correct |
63 |
Correct |
312 ms |
51672 KB |
Output is correct |
64 |
Correct |
449 ms |
106060 KB |
Output is correct |
65 |
Correct |
426 ms |
103744 KB |
Output is correct |
66 |
Correct |
294 ms |
82976 KB |
Output is correct |
67 |
Correct |
182 ms |
44156 KB |
Output is correct |
68 |
Correct |
326 ms |
67176 KB |
Output is correct |
69 |
Correct |
350 ms |
72076 KB |
Output is correct |
70 |
Correct |
336 ms |
64304 KB |
Output is correct |