#include "race.h"
#include "bits/stdc++.h"
using namespace std;
#define rep(i,a,b) for(int i=(a); i<(b); ++i)
#define all(x) x.begin(),x.end()
#define sz(x) int(x.size())
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
const int Nmax = 2e5+10, oo = 1e9;
int cc = 0;
map<ll,ll> mps[Nmax]; // min dist, node id
struct state{
ll min_edge = oo;
ll offset_dist = 0, offset_edges = 0;
int map_id;
state(int at){
mps[cc][0] = 0;
map_id = cc++;
}
state(){
map_id = Nmax-1;
}
void prop(int len){
offset_edges++;
offset_dist+=len;
}
};
int best_path(int N, int K, int H[][2], int L[])
{
vector<vector<pair<int,ll>>> adj(N);
rep(i,0,N-1){
int u = H[i][0], v = H[i][1];
adj[u].push_back({v,L[i]});
adj[v].push_back({u,L[i]});
}
vi vis(N);
auto dfs = [&](int at, auto&& dfs) -> state {
vis[at] = 1;
if (sz(adj[at]) == 1 and vis[adj[at][0].first]) return state(at); // leaf
state my;
bool frst = 1;
for(auto [to,d] : adj[at]) if (!vis[to]){
if (frst){
my = dfs(to,dfs);
my.prop(d);
if (mps[my.map_id].count(-my.offset_dist)){
auto& cur = mps[my.map_id][-my.offset_dist];
cur = min(cur,-my.offset_edges);
}else{
mps[my.map_id][-my.offset_dist] = -my.offset_edges;
}
frst = 0;
}else{
auto ot = dfs(to,dfs);
ot.prop(d);
// merge
if (sz(mps[ot.map_id]) > sz(mps[my.map_id])) swap(ot,my);
ll diff = ot.offset_dist - my.offset_dist;
ll diffedges = ot.offset_edges - my.offset_edges;
if (ot.min_edge < my.min_edge){ // update best until now
my.min_edge = ot.min_edge;
}
// find new best
for(auto [k,v] : mps[ot.map_id]){
ll finddist = K-k-ot.offset_dist-my.offset_dist;
if (mps[my.map_id].count(finddist)){
auto ret = mps[my.map_id][finddist];
int newedge = my.offset_edges + ot.offset_edges + v + ret;
if (newedge < my.min_edge){
my.min_edge = newedge;
}
}
}
// merge
for(auto [k,v] : mps[ot.map_id]){
ll newdist = k + diff;
int newedge = v + diffedges;
if (mps[my.map_id].count(newdist)){
auto& ret = mps[my.map_id][newdist];
if (ret > newedge){
ret = newedge;
}
}else{
mps[my.map_id][newdist] = newedge;
}
}
}
}
// find new best, on top
{
ll finddist = K - my.offset_dist;
if (mps[my.map_id].count(finddist)){
auto ret = mps[my.map_id][finddist] + my.offset_edges;
if (ret < my.min_edge){
my.min_edge = ret;
}
}
}
return my;
};
auto ret = dfs(0,dfs);
if (ret.min_edge == oo) return -1;
else return ret.min_edge;
}
// int H_in[200][2], L_in[200];
// int main(){
// cin.tie(NULL), cin.sync_with_stdio(false);
// int n,k; cin >> n >> k;
// rep(i,0,n-1){
// int u,v,l; cin >> u >> v >> l;
// H_in[i][0] = u;
// H_in[i][1] = v;
// L_in[i] = l;
// }
// cout << best_path(n,k,H_in,L_in) << '\n';
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13400 KB |
Output is correct |
2 |
Correct |
2 ms |
13404 KB |
Output is correct |
3 |
Correct |
2 ms |
13404 KB |
Output is correct |
4 |
Correct |
2 ms |
13404 KB |
Output is correct |
5 |
Correct |
2 ms |
13480 KB |
Output is correct |
6 |
Correct |
2 ms |
13404 KB |
Output is correct |
7 |
Correct |
2 ms |
13400 KB |
Output is correct |
8 |
Correct |
2 ms |
13516 KB |
Output is correct |
9 |
Correct |
2 ms |
13400 KB |
Output is correct |
10 |
Correct |
3 ms |
13400 KB |
Output is correct |
11 |
Correct |
2 ms |
13404 KB |
Output is correct |
12 |
Correct |
2 ms |
13508 KB |
Output is correct |
13 |
Correct |
2 ms |
13404 KB |
Output is correct |
14 |
Correct |
2 ms |
13404 KB |
Output is correct |
15 |
Correct |
2 ms |
13404 KB |
Output is correct |
16 |
Correct |
2 ms |
13404 KB |
Output is correct |
17 |
Correct |
2 ms |
13400 KB |
Output is correct |
18 |
Correct |
2 ms |
13404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13400 KB |
Output is correct |
2 |
Correct |
2 ms |
13404 KB |
Output is correct |
3 |
Correct |
2 ms |
13404 KB |
Output is correct |
4 |
Correct |
2 ms |
13404 KB |
Output is correct |
5 |
Correct |
2 ms |
13480 KB |
Output is correct |
6 |
Correct |
2 ms |
13404 KB |
Output is correct |
7 |
Correct |
2 ms |
13400 KB |
Output is correct |
8 |
Correct |
2 ms |
13516 KB |
Output is correct |
9 |
Correct |
2 ms |
13400 KB |
Output is correct |
10 |
Correct |
3 ms |
13400 KB |
Output is correct |
11 |
Correct |
2 ms |
13404 KB |
Output is correct |
12 |
Correct |
2 ms |
13508 KB |
Output is correct |
13 |
Correct |
2 ms |
13404 KB |
Output is correct |
14 |
Correct |
2 ms |
13404 KB |
Output is correct |
15 |
Correct |
2 ms |
13404 KB |
Output is correct |
16 |
Correct |
2 ms |
13404 KB |
Output is correct |
17 |
Correct |
2 ms |
13400 KB |
Output is correct |
18 |
Correct |
2 ms |
13404 KB |
Output is correct |
19 |
Correct |
2 ms |
13656 KB |
Output is correct |
20 |
Correct |
3 ms |
13404 KB |
Output is correct |
21 |
Correct |
3 ms |
13660 KB |
Output is correct |
22 |
Correct |
3 ms |
13756 KB |
Output is correct |
23 |
Correct |
2 ms |
13660 KB |
Output is correct |
24 |
Correct |
3 ms |
13660 KB |
Output is correct |
25 |
Correct |
2 ms |
13660 KB |
Output is correct |
26 |
Correct |
3 ms |
13804 KB |
Output is correct |
27 |
Correct |
2 ms |
13404 KB |
Output is correct |
28 |
Correct |
3 ms |
13660 KB |
Output is correct |
29 |
Correct |
3 ms |
13660 KB |
Output is correct |
30 |
Correct |
3 ms |
13660 KB |
Output is correct |
31 |
Correct |
3 ms |
13660 KB |
Output is correct |
32 |
Correct |
2 ms |
13660 KB |
Output is correct |
33 |
Correct |
3 ms |
13660 KB |
Output is correct |
34 |
Correct |
2 ms |
13660 KB |
Output is correct |
35 |
Correct |
3 ms |
13660 KB |
Output is correct |
36 |
Correct |
2 ms |
13660 KB |
Output is correct |
37 |
Correct |
2 ms |
13504 KB |
Output is correct |
38 |
Correct |
3 ms |
13660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13400 KB |
Output is correct |
2 |
Correct |
2 ms |
13404 KB |
Output is correct |
3 |
Correct |
2 ms |
13404 KB |
Output is correct |
4 |
Correct |
2 ms |
13404 KB |
Output is correct |
5 |
Correct |
2 ms |
13480 KB |
Output is correct |
6 |
Correct |
2 ms |
13404 KB |
Output is correct |
7 |
Correct |
2 ms |
13400 KB |
Output is correct |
8 |
Correct |
2 ms |
13516 KB |
Output is correct |
9 |
Correct |
2 ms |
13400 KB |
Output is correct |
10 |
Correct |
3 ms |
13400 KB |
Output is correct |
11 |
Correct |
2 ms |
13404 KB |
Output is correct |
12 |
Correct |
2 ms |
13508 KB |
Output is correct |
13 |
Correct |
2 ms |
13404 KB |
Output is correct |
14 |
Correct |
2 ms |
13404 KB |
Output is correct |
15 |
Correct |
2 ms |
13404 KB |
Output is correct |
16 |
Correct |
2 ms |
13404 KB |
Output is correct |
17 |
Correct |
2 ms |
13400 KB |
Output is correct |
18 |
Correct |
2 ms |
13404 KB |
Output is correct |
19 |
Correct |
64 ms |
36760 KB |
Output is correct |
20 |
Correct |
76 ms |
36832 KB |
Output is correct |
21 |
Correct |
65 ms |
36688 KB |
Output is correct |
22 |
Correct |
71 ms |
36436 KB |
Output is correct |
23 |
Correct |
119 ms |
49492 KB |
Output is correct |
24 |
Correct |
77 ms |
41664 KB |
Output is correct |
25 |
Correct |
60 ms |
38996 KB |
Output is correct |
26 |
Correct |
47 ms |
51028 KB |
Output is correct |
27 |
Correct |
119 ms |
47700 KB |
Output is correct |
28 |
Correct |
183 ms |
99152 KB |
Output is correct |
29 |
Correct |
171 ms |
96188 KB |
Output is correct |
30 |
Correct |
107 ms |
47668 KB |
Output is correct |
31 |
Correct |
115 ms |
47676 KB |
Output is correct |
32 |
Correct |
125 ms |
48060 KB |
Output is correct |
33 |
Correct |
119 ms |
46416 KB |
Output is correct |
34 |
Correct |
225 ms |
79404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13400 KB |
Output is correct |
2 |
Correct |
2 ms |
13404 KB |
Output is correct |
3 |
Correct |
2 ms |
13404 KB |
Output is correct |
4 |
Correct |
2 ms |
13404 KB |
Output is correct |
5 |
Correct |
2 ms |
13480 KB |
Output is correct |
6 |
Correct |
2 ms |
13404 KB |
Output is correct |
7 |
Correct |
2 ms |
13400 KB |
Output is correct |
8 |
Correct |
2 ms |
13516 KB |
Output is correct |
9 |
Correct |
2 ms |
13400 KB |
Output is correct |
10 |
Correct |
3 ms |
13400 KB |
Output is correct |
11 |
Correct |
2 ms |
13404 KB |
Output is correct |
12 |
Correct |
2 ms |
13508 KB |
Output is correct |
13 |
Correct |
2 ms |
13404 KB |
Output is correct |
14 |
Correct |
2 ms |
13404 KB |
Output is correct |
15 |
Correct |
2 ms |
13404 KB |
Output is correct |
16 |
Correct |
2 ms |
13404 KB |
Output is correct |
17 |
Correct |
2 ms |
13400 KB |
Output is correct |
18 |
Correct |
2 ms |
13404 KB |
Output is correct |
19 |
Correct |
2 ms |
13656 KB |
Output is correct |
20 |
Correct |
3 ms |
13404 KB |
Output is correct |
21 |
Correct |
3 ms |
13660 KB |
Output is correct |
22 |
Correct |
3 ms |
13756 KB |
Output is correct |
23 |
Correct |
2 ms |
13660 KB |
Output is correct |
24 |
Correct |
3 ms |
13660 KB |
Output is correct |
25 |
Correct |
2 ms |
13660 KB |
Output is correct |
26 |
Correct |
3 ms |
13804 KB |
Output is correct |
27 |
Correct |
2 ms |
13404 KB |
Output is correct |
28 |
Correct |
3 ms |
13660 KB |
Output is correct |
29 |
Correct |
3 ms |
13660 KB |
Output is correct |
30 |
Correct |
3 ms |
13660 KB |
Output is correct |
31 |
Correct |
3 ms |
13660 KB |
Output is correct |
32 |
Correct |
2 ms |
13660 KB |
Output is correct |
33 |
Correct |
3 ms |
13660 KB |
Output is correct |
34 |
Correct |
2 ms |
13660 KB |
Output is correct |
35 |
Correct |
3 ms |
13660 KB |
Output is correct |
36 |
Correct |
2 ms |
13660 KB |
Output is correct |
37 |
Correct |
2 ms |
13504 KB |
Output is correct |
38 |
Correct |
3 ms |
13660 KB |
Output is correct |
39 |
Correct |
64 ms |
36760 KB |
Output is correct |
40 |
Correct |
76 ms |
36832 KB |
Output is correct |
41 |
Correct |
65 ms |
36688 KB |
Output is correct |
42 |
Correct |
71 ms |
36436 KB |
Output is correct |
43 |
Correct |
119 ms |
49492 KB |
Output is correct |
44 |
Correct |
77 ms |
41664 KB |
Output is correct |
45 |
Correct |
60 ms |
38996 KB |
Output is correct |
46 |
Correct |
47 ms |
51028 KB |
Output is correct |
47 |
Correct |
119 ms |
47700 KB |
Output is correct |
48 |
Correct |
183 ms |
99152 KB |
Output is correct |
49 |
Correct |
171 ms |
96188 KB |
Output is correct |
50 |
Correct |
107 ms |
47668 KB |
Output is correct |
51 |
Correct |
115 ms |
47676 KB |
Output is correct |
52 |
Correct |
125 ms |
48060 KB |
Output is correct |
53 |
Correct |
119 ms |
46416 KB |
Output is correct |
54 |
Correct |
225 ms |
79404 KB |
Output is correct |
55 |
Correct |
13 ms |
16476 KB |
Output is correct |
56 |
Correct |
7 ms |
15196 KB |
Output is correct |
57 |
Correct |
47 ms |
34616 KB |
Output is correct |
58 |
Correct |
38 ms |
30368 KB |
Output is correct |
59 |
Correct |
69 ms |
57168 KB |
Output is correct |
60 |
Correct |
182 ms |
96952 KB |
Output is correct |
61 |
Correct |
134 ms |
51024 KB |
Output is correct |
62 |
Correct |
114 ms |
47700 KB |
Output is correct |
63 |
Correct |
108 ms |
47700 KB |
Output is correct |
64 |
Correct |
298 ms |
101460 KB |
Output is correct |
65 |
Correct |
272 ms |
100536 KB |
Output is correct |
66 |
Correct |
177 ms |
89424 KB |
Output is correct |
67 |
Correct |
85 ms |
47248 KB |
Output is correct |
68 |
Correct |
190 ms |
66132 KB |
Output is correct |
69 |
Correct |
220 ms |
70888 KB |
Output is correct |
70 |
Correct |
187 ms |
64000 KB |
Output is correct |