#include "race.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
#define fi first
#define se second
const int INF = 1e9+1;
const int P = 1000000007;
const ll LLINF = (ll)1e18+1;
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) { os << p.fi << " " << p.se; return os; }
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) { for(auto i : v) os << i << " "; os << "\n"; return os; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rnd(x, y) uniform_int_distribution<int>(x, y)(rng)
ll mod(ll a, ll b) { return ((a%b) + b) % b; }
ll ext_gcd(ll a, ll b, ll &x, ll &y) {
ll g = a; x = 1, y = 0;
if(b) g = ext_gcd(b, a % b, y, x), y -= a / b * x;
return g;
}
ll inv(ll a, ll m) {
ll x, y; ll g = ext_gcd(a, m, x, y);
if(g > 1) return -1;
return mod(x, m);
}
int dfs(int x, ll d, int l, vector<vector<pii>> &E,
vector<int> &len, vector<int> &siz, vector<ll> &dep) {
siz[x] = 1; len[x] = l; dep[x] = d;
for(auto &i : E[x]) {
E[i.fi].erase(find(E[i.fi].begin(), E[i.fi].end(), pii(x, i.se)));
siz[x] += dfs(i.fi, d+i.se, l+1, E, len, siz, dep);
if(siz[i.fi] > siz[E[x][0].fi]) swap(i, E[x][0]);
}
return siz[x];
}
int val(set<pll> *S, ll k, const pll &x, const pll &i) {
ll tmp = k+2*x.fi-i.fi;
auto iter = S->lower_bound({tmp, 0LL});
if(iter == S->end() || iter->fi != tmp) return INF;
return iter->se+i.se-2*x.se;
}
int solve(int x, ll k, vector<vector<pii>> &E,
vector<int> &len, vector<int> &siz, vector<ll> &dep, vector<set<pll>*> &S) {
if(E[x].empty()) {
S[x] = new set<pll>;
S[x]->insert({dep[x], len[x]});
return INF;
}
int ans = INF;
for(auto &i : E[x]) ans = min(ans, solve(i.fi, k, E, len, siz, dep, S));
S[x] = S[E[x][0].fi];
pll tmp = {dep[x], len[x]};
for(int i = 1; i < E[x].size(); i++) {
for(auto &j : *S[E[x][i].fi]) ans = min(ans, val(S[x], k, tmp, j));
for(auto &j : *S[E[x][i].fi]) S[x]->insert(j);
}
ans = min(ans, val(S[x], k, tmp, tmp));
S[x]->insert(tmp);
return ans;
}
int best_path(int N, int K, int H[][2], int L[]) {
vector<vector<pii>> E(N);
for(int i = 0; i+1 < N; i++) {
E[H[i][0]].push_back({H[i][1], L[i]});
E[H[i][1]].push_back({H[i][0], L[i]});
}
vector<ll> dep(N);
vector<int> len(N), siz(N);
dfs(0, 0LL, 0, E, len, siz, dep);
vector<set<pll>*> S(N, nullptr);
int ans = solve(0, K, E, len, siz, dep, S);
if(ans == INF) return -1;
else return ans;
}
Compilation message
race.cpp: In function 'int solve(int, ll, std::vector<std::vector<std::pair<int, int> > >&, std::vector<int>&, std::vector<int>&, std::vector<long long int>&, std::vector<std::set<std::pair<long long int, long long int> >*>&)':
race.cpp:63:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i = 1; i < E[x].size(); i++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 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 |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
3 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
584 KB |
Output is correct |
26 |
Correct |
2 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 |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
2 ms |
580 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
2 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
2 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 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 |
113 ms |
29768 KB |
Output is correct |
20 |
Correct |
118 ms |
29548 KB |
Output is correct |
21 |
Correct |
116 ms |
29248 KB |
Output is correct |
22 |
Correct |
122 ms |
28912 KB |
Output is correct |
23 |
Correct |
132 ms |
44344 KB |
Output is correct |
24 |
Correct |
99 ms |
35124 KB |
Output is correct |
25 |
Correct |
116 ms |
28856 KB |
Output is correct |
26 |
Correct |
72 ms |
34096 KB |
Output is correct |
27 |
Correct |
185 ms |
44616 KB |
Output is correct |
28 |
Correct |
227 ms |
68168 KB |
Output is correct |
29 |
Correct |
236 ms |
66668 KB |
Output is correct |
30 |
Correct |
178 ms |
44608 KB |
Output is correct |
31 |
Correct |
185 ms |
44504 KB |
Output is correct |
32 |
Correct |
253 ms |
44680 KB |
Output is correct |
33 |
Correct |
181 ms |
34204 KB |
Output is correct |
34 |
Correct |
291 ms |
67076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 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 |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
3 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
584 KB |
Output is correct |
26 |
Correct |
2 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 |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
2 ms |
580 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
2 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
2 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
113 ms |
29768 KB |
Output is correct |
40 |
Correct |
118 ms |
29548 KB |
Output is correct |
41 |
Correct |
116 ms |
29248 KB |
Output is correct |
42 |
Correct |
122 ms |
28912 KB |
Output is correct |
43 |
Correct |
132 ms |
44344 KB |
Output is correct |
44 |
Correct |
99 ms |
35124 KB |
Output is correct |
45 |
Correct |
116 ms |
28856 KB |
Output is correct |
46 |
Correct |
72 ms |
34096 KB |
Output is correct |
47 |
Correct |
185 ms |
44616 KB |
Output is correct |
48 |
Correct |
227 ms |
68168 KB |
Output is correct |
49 |
Correct |
236 ms |
66668 KB |
Output is correct |
50 |
Correct |
178 ms |
44608 KB |
Output is correct |
51 |
Correct |
185 ms |
44504 KB |
Output is correct |
52 |
Correct |
253 ms |
44680 KB |
Output is correct |
53 |
Correct |
181 ms |
34204 KB |
Output is correct |
54 |
Correct |
291 ms |
67076 KB |
Output is correct |
55 |
Correct |
11 ms |
4044 KB |
Output is correct |
56 |
Correct |
8 ms |
2772 KB |
Output is correct |
57 |
Correct |
65 ms |
26664 KB |
Output is correct |
58 |
Correct |
52 ms |
23656 KB |
Output is correct |
59 |
Correct |
69 ms |
34120 KB |
Output is correct |
60 |
Correct |
221 ms |
67024 KB |
Output is correct |
61 |
Correct |
219 ms |
50416 KB |
Output is correct |
62 |
Correct |
181 ms |
44668 KB |
Output is correct |
63 |
Correct |
215 ms |
44656 KB |
Output is correct |
64 |
Correct |
382 ms |
97488 KB |
Output is correct |
65 |
Correct |
387 ms |
92984 KB |
Output is correct |
66 |
Correct |
262 ms |
63112 KB |
Output is correct |
67 |
Correct |
161 ms |
48172 KB |
Output is correct |
68 |
Correct |
280 ms |
63864 KB |
Output is correct |
69 |
Correct |
274 ms |
64668 KB |
Output is correct |
70 |
Correct |
270 ms |
60956 KB |
Output is correct |