#include "race.h"
#include <bits/stdc++.h>
#define FIXED_FLOAT(x) std::fixed <<std::setprecision(20) << (x)
#define all(v) (v).begin(), (v).end()
using namespace std;
#define forn(i,n) for (int i = 0; i < (n); ++i)
#define rforn(i, n) for(int i = (n) - 1;i >= 0;--i)
#define sz(x) (int)x.size()
using ll = long long;
int mod = (ll)1e9 + 7;
#define PI acos(-1)
typedef pair<int, int> pairs;
const int INF = 1e9 + 1;
const int MX = 2e5 + 100;
const double eps = 1e-7;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template<class T, size_t SZ> using AR = array<T, SZ>;
template<class T> using PR = pair<T, T>;
template <typename XPAX>
bool ckma(XPAX &x, XPAX y) {
return (x < y ? x = y, 1 : 0);
}
template <typename XPAX>
bool ckmi(XPAX &x, XPAX y) {
return (x > y ? x = y, 1 : 0);
}
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
V<ll> dis(MX);
V<int> ms(MX);
VV<pair<int, ll>> g(MX);
V<int> dep(MX);
int n;
ll k;
V<int> tin(MX), tout(MX);
int T = 1;
int ret = INF;
multiset<int> Bs[MX];
V<int> val(MX);
V<int> mp(MX);
map<ll, int> UD;
int get_id(ll x) {
if(UD[x])
return UD[x];
else return -1;
}
void dfs1(int v, int p) {
tin[v] = T++;
mp[T - 1] = v;
dep[v] = (p == 0 ? 0 : dep[p] + 1);
for(auto [c, w] : g[v]) {
if(c != p) {
dis[c] =dis[v] + w;
dfs1(c, v);
}
}
tout[v] = T;
ms[v] = tout[v] - tin[v];
}
void dfs2(int v, int p, bool keep) {
int mx = -1, bg = -1;
for(auto [c, w] : g[v])
if(c != p && ms[c] > mx)
mx = ms[c], bg = c;
for(auto [c, w] : g[v])
if(c != p && c != bg)
dfs2(c, v, 0);
if(bg != -1)
dfs2(bg, v, 1);
for(auto [c, w] : g[v]) {
if(c != p && c != bg) {
//debug(v, c);
for(int x = tin[c];x < tout[c];++x) {
if(dis[v] + k == dis[mp[x]]) {
ckmi(ret, dep[mp[x]] - dep[v]);
}
else if(dis[mp[x]] - dis[v] < k) {
ll rr = dis[mp[x]] - dis[v];
rr = k - rr;
rr += dis[v];
//assert(dis[v] + k > rr);
int id = get_id(rr);
if(id != -1 && sz(Bs[id]))
ckmi(ret, *Bs[id].begin() + dep[mp[x]] - 2 * dep[v]);
}
}
for(int x= tin[c];x < tout[c];++x)
Bs[val[mp[x]]].insert(dep[mp[x]]);
}
}
int prx = dis[v] + k;
prx = get_id(prx);
if(prx != -1 && sz(Bs[prx]))
ckmi(ret, *Bs[prx].begin() - dep[v]);
Bs[val[v]].insert(dep[v]);
if(keep == 0)
for(int x = tin[v];x < tout[v];++x)
Bs[val[mp[x]]].erase(Bs[val[mp[x]]].find(dep[mp[x]]));
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N;
k = K;
forn(i, N - 1) {
++H[i][0], ++H[i][1];
g[H[i][0]].push_back({H[i][1], L[i]});
g[H[i][1]].push_back({H[i][0], L[i]});
}
dfs1(1, 0);
int cor = 1;
for(int i = 1;i <= n;++i)
val[i] = (UD[dis[i]] ? UD[dis[i]] : UD[dis[i]] = cor++);
dfs2(1, 0, 1);
if(ret == INF)
ret = -1;
return ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
20684 KB |
Output is correct |
2 |
Correct |
10 ms |
20684 KB |
Output is correct |
3 |
Correct |
13 ms |
20680 KB |
Output is correct |
4 |
Correct |
11 ms |
20684 KB |
Output is correct |
5 |
Correct |
10 ms |
20684 KB |
Output is correct |
6 |
Correct |
10 ms |
20684 KB |
Output is correct |
7 |
Correct |
10 ms |
20652 KB |
Output is correct |
8 |
Correct |
10 ms |
20684 KB |
Output is correct |
9 |
Correct |
10 ms |
20684 KB |
Output is correct |
10 |
Correct |
11 ms |
20724 KB |
Output is correct |
11 |
Correct |
11 ms |
20684 KB |
Output is correct |
12 |
Correct |
10 ms |
20684 KB |
Output is correct |
13 |
Correct |
12 ms |
20684 KB |
Output is correct |
14 |
Correct |
11 ms |
20692 KB |
Output is correct |
15 |
Correct |
10 ms |
20684 KB |
Output is correct |
16 |
Correct |
13 ms |
20732 KB |
Output is correct |
17 |
Correct |
10 ms |
20684 KB |
Output is correct |
18 |
Correct |
14 ms |
20716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
20684 KB |
Output is correct |
2 |
Correct |
10 ms |
20684 KB |
Output is correct |
3 |
Correct |
13 ms |
20680 KB |
Output is correct |
4 |
Correct |
11 ms |
20684 KB |
Output is correct |
5 |
Correct |
10 ms |
20684 KB |
Output is correct |
6 |
Correct |
10 ms |
20684 KB |
Output is correct |
7 |
Correct |
10 ms |
20652 KB |
Output is correct |
8 |
Correct |
10 ms |
20684 KB |
Output is correct |
9 |
Correct |
10 ms |
20684 KB |
Output is correct |
10 |
Correct |
11 ms |
20724 KB |
Output is correct |
11 |
Correct |
11 ms |
20684 KB |
Output is correct |
12 |
Correct |
10 ms |
20684 KB |
Output is correct |
13 |
Correct |
12 ms |
20684 KB |
Output is correct |
14 |
Correct |
11 ms |
20692 KB |
Output is correct |
15 |
Correct |
10 ms |
20684 KB |
Output is correct |
16 |
Correct |
13 ms |
20732 KB |
Output is correct |
17 |
Correct |
10 ms |
20684 KB |
Output is correct |
18 |
Correct |
14 ms |
20716 KB |
Output is correct |
19 |
Correct |
11 ms |
20624 KB |
Output is correct |
20 |
Correct |
10 ms |
20684 KB |
Output is correct |
21 |
Correct |
12 ms |
20864 KB |
Output is correct |
22 |
Correct |
12 ms |
20940 KB |
Output is correct |
23 |
Correct |
13 ms |
20852 KB |
Output is correct |
24 |
Correct |
12 ms |
20996 KB |
Output is correct |
25 |
Correct |
13 ms |
21032 KB |
Output is correct |
26 |
Correct |
12 ms |
20912 KB |
Output is correct |
27 |
Correct |
11 ms |
20700 KB |
Output is correct |
28 |
Correct |
13 ms |
21068 KB |
Output is correct |
29 |
Correct |
12 ms |
21032 KB |
Output is correct |
30 |
Correct |
15 ms |
21068 KB |
Output is correct |
31 |
Correct |
13 ms |
21084 KB |
Output is correct |
32 |
Correct |
16 ms |
21068 KB |
Output is correct |
33 |
Correct |
14 ms |
21092 KB |
Output is correct |
34 |
Correct |
12 ms |
20868 KB |
Output is correct |
35 |
Correct |
13 ms |
20948 KB |
Output is correct |
36 |
Correct |
11 ms |
20940 KB |
Output is correct |
37 |
Correct |
11 ms |
20940 KB |
Output is correct |
38 |
Correct |
11 ms |
20988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
20684 KB |
Output is correct |
2 |
Correct |
10 ms |
20684 KB |
Output is correct |
3 |
Correct |
13 ms |
20680 KB |
Output is correct |
4 |
Correct |
11 ms |
20684 KB |
Output is correct |
5 |
Correct |
10 ms |
20684 KB |
Output is correct |
6 |
Correct |
10 ms |
20684 KB |
Output is correct |
7 |
Correct |
10 ms |
20652 KB |
Output is correct |
8 |
Correct |
10 ms |
20684 KB |
Output is correct |
9 |
Correct |
10 ms |
20684 KB |
Output is correct |
10 |
Correct |
11 ms |
20724 KB |
Output is correct |
11 |
Correct |
11 ms |
20684 KB |
Output is correct |
12 |
Correct |
10 ms |
20684 KB |
Output is correct |
13 |
Correct |
12 ms |
20684 KB |
Output is correct |
14 |
Correct |
11 ms |
20692 KB |
Output is correct |
15 |
Correct |
10 ms |
20684 KB |
Output is correct |
16 |
Correct |
13 ms |
20732 KB |
Output is correct |
17 |
Correct |
10 ms |
20684 KB |
Output is correct |
18 |
Correct |
14 ms |
20716 KB |
Output is correct |
19 |
Correct |
240 ms |
31616 KB |
Output is correct |
20 |
Correct |
285 ms |
31764 KB |
Output is correct |
21 |
Correct |
236 ms |
31632 KB |
Output is correct |
22 |
Correct |
248 ms |
31652 KB |
Output is correct |
23 |
Correct |
234 ms |
32124 KB |
Output is correct |
24 |
Correct |
188 ms |
31300 KB |
Output is correct |
25 |
Correct |
139 ms |
41436 KB |
Output is correct |
26 |
Correct |
82 ms |
49988 KB |
Output is correct |
27 |
Correct |
276 ms |
43428 KB |
Output is correct |
28 |
Correct |
456 ms |
104204 KB |
Output is correct |
29 |
Correct |
457 ms |
101364 KB |
Output is correct |
30 |
Correct |
313 ms |
43316 KB |
Output is correct |
31 |
Correct |
314 ms |
43304 KB |
Output is correct |
32 |
Correct |
390 ms |
43716 KB |
Output is correct |
33 |
Correct |
361 ms |
42468 KB |
Output is correct |
34 |
Correct |
733 ms |
67136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
20684 KB |
Output is correct |
2 |
Correct |
10 ms |
20684 KB |
Output is correct |
3 |
Correct |
13 ms |
20680 KB |
Output is correct |
4 |
Correct |
11 ms |
20684 KB |
Output is correct |
5 |
Correct |
10 ms |
20684 KB |
Output is correct |
6 |
Correct |
10 ms |
20684 KB |
Output is correct |
7 |
Correct |
10 ms |
20652 KB |
Output is correct |
8 |
Correct |
10 ms |
20684 KB |
Output is correct |
9 |
Correct |
10 ms |
20684 KB |
Output is correct |
10 |
Correct |
11 ms |
20724 KB |
Output is correct |
11 |
Correct |
11 ms |
20684 KB |
Output is correct |
12 |
Correct |
10 ms |
20684 KB |
Output is correct |
13 |
Correct |
12 ms |
20684 KB |
Output is correct |
14 |
Correct |
11 ms |
20692 KB |
Output is correct |
15 |
Correct |
10 ms |
20684 KB |
Output is correct |
16 |
Correct |
13 ms |
20732 KB |
Output is correct |
17 |
Correct |
10 ms |
20684 KB |
Output is correct |
18 |
Correct |
14 ms |
20716 KB |
Output is correct |
19 |
Correct |
11 ms |
20624 KB |
Output is correct |
20 |
Correct |
10 ms |
20684 KB |
Output is correct |
21 |
Correct |
12 ms |
20864 KB |
Output is correct |
22 |
Correct |
12 ms |
20940 KB |
Output is correct |
23 |
Correct |
13 ms |
20852 KB |
Output is correct |
24 |
Correct |
12 ms |
20996 KB |
Output is correct |
25 |
Correct |
13 ms |
21032 KB |
Output is correct |
26 |
Correct |
12 ms |
20912 KB |
Output is correct |
27 |
Correct |
11 ms |
20700 KB |
Output is correct |
28 |
Correct |
13 ms |
21068 KB |
Output is correct |
29 |
Correct |
12 ms |
21032 KB |
Output is correct |
30 |
Correct |
15 ms |
21068 KB |
Output is correct |
31 |
Correct |
13 ms |
21084 KB |
Output is correct |
32 |
Correct |
16 ms |
21068 KB |
Output is correct |
33 |
Correct |
14 ms |
21092 KB |
Output is correct |
34 |
Correct |
12 ms |
20868 KB |
Output is correct |
35 |
Correct |
13 ms |
20948 KB |
Output is correct |
36 |
Correct |
11 ms |
20940 KB |
Output is correct |
37 |
Correct |
11 ms |
20940 KB |
Output is correct |
38 |
Correct |
11 ms |
20988 KB |
Output is correct |
39 |
Correct |
240 ms |
31616 KB |
Output is correct |
40 |
Correct |
285 ms |
31764 KB |
Output is correct |
41 |
Correct |
236 ms |
31632 KB |
Output is correct |
42 |
Correct |
248 ms |
31652 KB |
Output is correct |
43 |
Correct |
234 ms |
32124 KB |
Output is correct |
44 |
Correct |
188 ms |
31300 KB |
Output is correct |
45 |
Correct |
139 ms |
41436 KB |
Output is correct |
46 |
Correct |
82 ms |
49988 KB |
Output is correct |
47 |
Correct |
276 ms |
43428 KB |
Output is correct |
48 |
Correct |
456 ms |
104204 KB |
Output is correct |
49 |
Correct |
457 ms |
101364 KB |
Output is correct |
50 |
Correct |
313 ms |
43316 KB |
Output is correct |
51 |
Correct |
314 ms |
43304 KB |
Output is correct |
52 |
Correct |
390 ms |
43716 KB |
Output is correct |
53 |
Correct |
361 ms |
42468 KB |
Output is correct |
54 |
Correct |
733 ms |
67136 KB |
Output is correct |
55 |
Correct |
32 ms |
22692 KB |
Output is correct |
56 |
Correct |
29 ms |
21820 KB |
Output is correct |
57 |
Correct |
236 ms |
33260 KB |
Output is correct |
58 |
Correct |
102 ms |
32276 KB |
Output is correct |
59 |
Correct |
148 ms |
63644 KB |
Output is correct |
60 |
Correct |
433 ms |
105428 KB |
Output is correct |
61 |
Correct |
337 ms |
48480 KB |
Output is correct |
62 |
Correct |
344 ms |
46752 KB |
Output is correct |
63 |
Correct |
491 ms |
46852 KB |
Output is correct |
64 |
Correct |
1088 ms |
74132 KB |
Output is correct |
65 |
Correct |
817 ms |
70892 KB |
Output is correct |
66 |
Correct |
435 ms |
98468 KB |
Output is correct |
67 |
Correct |
319 ms |
45396 KB |
Output is correct |
68 |
Correct |
952 ms |
81352 KB |
Output is correct |
69 |
Correct |
984 ms |
81724 KB |
Output is correct |
70 |
Correct |
934 ms |
78980 KB |
Output is correct |