#include "race.h"
#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 2e5+1, oo = 1e9;
static int ans,k;
struct paths {
map<ll,int> m;
ll delta=0;
int extraedge=0;
int get(ll l) {
ll tmp = l-delta;
auto iter = m.find(tmp);
if(iter!=m.end()) return iter->second+extraedge;
return oo;
}
void insert(ll l, int i) {
i-=extraedge;
ll tmp = l-delta;
auto iter = m.find(tmp);
if(iter!=m.end()) iter->second = min(i,iter->second);
else m[tmp] = i;
}
};
void merge(paths& a, paths& b) {
if(a.m.size()<b.m.size()) swap(a,b);
for(auto& p: b.m) {
int real = p.first+b.delta;
ans = min(ans,p.second+b.extraedge+a.get(k-real));
}
for(auto& p: b.m) {
auto real = p.first+b.delta;
a.insert(real,p.second+b.extraedge);
}
}
vector<pi> adj[mxN];
void dfs(int at, int from, int ww, paths& parent) {
paths p;
p.insert(0,0);
for(auto [to,w]: adj[at]) {
if(to==from) continue;
dfs(to,at,w,p);
}
p.delta+=ww,p.extraedge++;
merge(parent,p);
}
int best_path(int N, int K, int H[][2], int L[]) {
int n = N;
k=K, ans = oo;
for(int i=0;i<n;++i) adj[i].clear();
for(int i=0;i<n-1;++i) {
int a = H[i][0], b = H[i][1], w = L[i];
adj[a].emplace_back(b,w);
adj[b].emplace_back(a,w);
}
paths empty;
dfs(0,-1,0,empty);
return ans==oo?-1:ans;
}
Compilation message
race.cpp: In function 'void dfs(int, int, int, paths&)':
race.cpp:48:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
48 | for(auto [to,w]: adj[at]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
5028 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
5028 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
5 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
5068 KB |
Output is correct |
24 |
Correct |
5 ms |
5068 KB |
Output is correct |
25 |
Correct |
4 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5132 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
5140 KB |
Output is correct |
29 |
Correct |
5 ms |
5004 KB |
Output is correct |
30 |
Correct |
5 ms |
5068 KB |
Output is correct |
31 |
Correct |
4 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5068 KB |
Output is correct |
33 |
Correct |
5 ms |
5068 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
4 ms |
5068 KB |
Output is correct |
36 |
Correct |
4 ms |
5068 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
5028 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
102 ms |
9932 KB |
Output is correct |
20 |
Correct |
94 ms |
9896 KB |
Output is correct |
21 |
Correct |
93 ms |
9840 KB |
Output is correct |
22 |
Correct |
90 ms |
9880 KB |
Output is correct |
23 |
Correct |
123 ms |
10340 KB |
Output is correct |
24 |
Correct |
101 ms |
10084 KB |
Output is correct |
25 |
Correct |
79 ms |
21056 KB |
Output is correct |
26 |
Correct |
70 ms |
32456 KB |
Output is correct |
27 |
Correct |
179 ms |
18088 KB |
Output is correct |
28 |
Correct |
255 ms |
60404 KB |
Output is correct |
29 |
Correct |
256 ms |
58948 KB |
Output is correct |
30 |
Correct |
180 ms |
18004 KB |
Output is correct |
31 |
Correct |
179 ms |
17988 KB |
Output is correct |
32 |
Correct |
199 ms |
18268 KB |
Output is correct |
33 |
Correct |
178 ms |
17476 KB |
Output is correct |
34 |
Correct |
327 ms |
30412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
5028 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
5 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
5068 KB |
Output is correct |
24 |
Correct |
5 ms |
5068 KB |
Output is correct |
25 |
Correct |
4 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5132 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
5140 KB |
Output is correct |
29 |
Correct |
5 ms |
5004 KB |
Output is correct |
30 |
Correct |
5 ms |
5068 KB |
Output is correct |
31 |
Correct |
4 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5068 KB |
Output is correct |
33 |
Correct |
5 ms |
5068 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
4 ms |
5068 KB |
Output is correct |
36 |
Correct |
4 ms |
5068 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
39 |
Correct |
102 ms |
9932 KB |
Output is correct |
40 |
Correct |
94 ms |
9896 KB |
Output is correct |
41 |
Correct |
93 ms |
9840 KB |
Output is correct |
42 |
Correct |
90 ms |
9880 KB |
Output is correct |
43 |
Correct |
123 ms |
10340 KB |
Output is correct |
44 |
Correct |
101 ms |
10084 KB |
Output is correct |
45 |
Correct |
79 ms |
21056 KB |
Output is correct |
46 |
Correct |
70 ms |
32456 KB |
Output is correct |
47 |
Correct |
179 ms |
18088 KB |
Output is correct |
48 |
Correct |
255 ms |
60404 KB |
Output is correct |
49 |
Correct |
256 ms |
58948 KB |
Output is correct |
50 |
Correct |
180 ms |
18004 KB |
Output is correct |
51 |
Correct |
179 ms |
17988 KB |
Output is correct |
52 |
Correct |
199 ms |
18268 KB |
Output is correct |
53 |
Correct |
178 ms |
17476 KB |
Output is correct |
54 |
Correct |
327 ms |
30412 KB |
Output is correct |
55 |
Correct |
17 ms |
5996 KB |
Output is correct |
56 |
Correct |
10 ms |
5524 KB |
Output is correct |
57 |
Correct |
63 ms |
11368 KB |
Output is correct |
58 |
Correct |
49 ms |
11096 KB |
Output is correct |
59 |
Correct |
102 ms |
32524 KB |
Output is correct |
60 |
Correct |
245 ms |
59332 KB |
Output is correct |
61 |
Correct |
208 ms |
19896 KB |
Output is correct |
62 |
Correct |
180 ms |
18076 KB |
Output is correct |
63 |
Correct |
194 ms |
18264 KB |
Output is correct |
64 |
Correct |
424 ms |
26484 KB |
Output is correct |
65 |
Correct |
424 ms |
30652 KB |
Output is correct |
66 |
Correct |
276 ms |
56004 KB |
Output is correct |
67 |
Correct |
132 ms |
18412 KB |
Output is correct |
68 |
Correct |
321 ms |
28980 KB |
Output is correct |
69 |
Correct |
343 ms |
33348 KB |
Output is correct |
70 |
Correct |
286 ms |
28100 KB |
Output is correct |