#include "race.h"
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define pb push_back
typedef int64_t lld;
typedef pair<int, int> pii;
/*
struct node;
vector<node*> nodes;
struct node{
node *L = 0, *R = 0;
lld l, r, mid;
int val = 0;
node(lld ll, lld rr) : l(ll), r(rr), mid((l+r)/2) {}
node(*node n){ L = n->L, R = n->R, l = n->l, r = n->r, mid = n->mid, val = n->val + 1; }
node* update(lld x){
node* n = new node(this);
if(l == r)return n;
if(x <= mid){
if(!n->L) n->L = new node(l, mid);
return n->L->update(x);
}else{
if(!n->R) n->R = new node(mid+1, r);
return n->R->update(x);
}
}
};
*/
vector<vector<pii>> G;
int getSize(int i, int p){
int sz = 1;
for(auto x: G[i])if(x.ff != p)sz += getSize(x.ff, i);
return sz;
}
int getCent(int i, int p, int n){
int sz = 1; bool fl = true;
for(auto x : G[i])
if(x.ff != p){
auto r = getCent(x.ff, i, n);
if(r >= 0)return r;
r = -r;
if(r > n/2)fl = false;
sz += r;
}
if(n-sz > n/2)fl = false;
if(fl)return i;
else return -sz;
}
map<int, int> deps, tdeps;
void dfs(int i, int p, int d, int c){
if(!deps[d])deps[d] = c;
else deps[d] = min(deps[d], c);
for(auto x : G[i])
if(x.ff != p)
dfs(x.ff, i, d+x.ss, c+1);
}
int process(int r, int targ){
//cout << r << endl;
deps.clear(); tdeps.clear(); tdeps[0] = 1;
int ans = INT_MAX;
for(auto x: G[r]){
G[x.ff].erase(remove(all(G[x.ff]), make_pair(r, x.ss)), G[x.ff].end());
dfs(x.ff, r, x.ss, 2);
for(auto i: deps)
if(tdeps.find(targ-i.ff) != tdeps.end())
ans = min(ans, tdeps[targ-i.ff]+i.ss-1);
for(auto i: deps)
if(!tdeps[i.ff])tdeps[i.ff] = i.ss;
else tdeps[i.ff] = min(tdeps[i.ff], i.ss);
deps.clear();
}
G[r].resize(0);
return ans;
}
int best_path(int N, int K, int H[][2], int L[]) {
G.resize(N);
for(int i = 0; i < N-1; i++)G[H[i][0]].pb({H[i][1], L[i]}), G[H[i][1]].pb({H[i][0], L[i]});
int ans = INT_MAX;
for(int i = 0; i < N; i++)while(G[i].size())ans = min(ans, process(getCent(i, i, getSize(i, i)), K));
return ans == INT_MAX?-1:(ans-1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
524 KB |
Output is correct |
23 |
Correct |
0 ms |
640 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
640 KB |
Output is correct |
26 |
Correct |
3 ms |
512 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
512 KB |
Output is correct |
29 |
Correct |
4 ms |
512 KB |
Output is correct |
30 |
Correct |
3 ms |
512 KB |
Output is correct |
31 |
Correct |
4 ms |
512 KB |
Output is correct |
32 |
Correct |
3 ms |
512 KB |
Output is correct |
33 |
Correct |
3 ms |
512 KB |
Output is correct |
34 |
Correct |
4 ms |
512 KB |
Output is correct |
35 |
Correct |
4 ms |
512 KB |
Output is correct |
36 |
Correct |
3 ms |
512 KB |
Output is correct |
37 |
Correct |
3 ms |
512 KB |
Output is correct |
38 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
447 ms |
8568 KB |
Output is correct |
20 |
Correct |
376 ms |
8568 KB |
Output is correct |
21 |
Correct |
356 ms |
8568 KB |
Output is correct |
22 |
Correct |
322 ms |
8684 KB |
Output is correct |
23 |
Correct |
495 ms |
8824 KB |
Output is correct |
24 |
Correct |
226 ms |
8696 KB |
Output is correct |
25 |
Correct |
258 ms |
13176 KB |
Output is correct |
26 |
Correct |
123 ms |
15864 KB |
Output is correct |
27 |
Correct |
506 ms |
16248 KB |
Output is correct |
28 |
Correct |
1630 ms |
44280 KB |
Output is correct |
29 |
Correct |
1653 ms |
43256 KB |
Output is correct |
30 |
Correct |
498 ms |
16120 KB |
Output is correct |
31 |
Correct |
513 ms |
16376 KB |
Output is correct |
32 |
Correct |
644 ms |
16120 KB |
Output is correct |
33 |
Correct |
843 ms |
14968 KB |
Output is correct |
34 |
Correct |
1543 ms |
26616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
524 KB |
Output is correct |
23 |
Correct |
0 ms |
640 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
640 KB |
Output is correct |
26 |
Correct |
3 ms |
512 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
512 KB |
Output is correct |
29 |
Correct |
4 ms |
512 KB |
Output is correct |
30 |
Correct |
3 ms |
512 KB |
Output is correct |
31 |
Correct |
4 ms |
512 KB |
Output is correct |
32 |
Correct |
3 ms |
512 KB |
Output is correct |
33 |
Correct |
3 ms |
512 KB |
Output is correct |
34 |
Correct |
4 ms |
512 KB |
Output is correct |
35 |
Correct |
4 ms |
512 KB |
Output is correct |
36 |
Correct |
3 ms |
512 KB |
Output is correct |
37 |
Correct |
3 ms |
512 KB |
Output is correct |
38 |
Correct |
3 ms |
512 KB |
Output is correct |
39 |
Correct |
447 ms |
8568 KB |
Output is correct |
40 |
Correct |
376 ms |
8568 KB |
Output is correct |
41 |
Correct |
356 ms |
8568 KB |
Output is correct |
42 |
Correct |
322 ms |
8684 KB |
Output is correct |
43 |
Correct |
495 ms |
8824 KB |
Output is correct |
44 |
Correct |
226 ms |
8696 KB |
Output is correct |
45 |
Correct |
258 ms |
13176 KB |
Output is correct |
46 |
Correct |
123 ms |
15864 KB |
Output is correct |
47 |
Correct |
506 ms |
16248 KB |
Output is correct |
48 |
Correct |
1630 ms |
44280 KB |
Output is correct |
49 |
Correct |
1653 ms |
43256 KB |
Output is correct |
50 |
Correct |
498 ms |
16120 KB |
Output is correct |
51 |
Correct |
513 ms |
16376 KB |
Output is correct |
52 |
Correct |
644 ms |
16120 KB |
Output is correct |
53 |
Correct |
843 ms |
14968 KB |
Output is correct |
54 |
Correct |
1543 ms |
26616 KB |
Output is correct |
55 |
Correct |
34 ms |
1536 KB |
Output is correct |
56 |
Correct |
19 ms |
1152 KB |
Output is correct |
57 |
Correct |
176 ms |
8896 KB |
Output is correct |
58 |
Correct |
63 ms |
8812 KB |
Output is correct |
59 |
Correct |
564 ms |
21752 KB |
Output is correct |
60 |
Correct |
1686 ms |
42872 KB |
Output is correct |
61 |
Correct |
572 ms |
17400 KB |
Output is correct |
62 |
Correct |
496 ms |
16248 KB |
Output is correct |
63 |
Correct |
632 ms |
16192 KB |
Output is correct |
64 |
Correct |
1734 ms |
22392 KB |
Output is correct |
65 |
Correct |
1482 ms |
25732 KB |
Output is correct |
66 |
Correct |
1704 ms |
40548 KB |
Output is correct |
67 |
Correct |
193 ms |
16232 KB |
Output is correct |
68 |
Correct |
883 ms |
24184 KB |
Output is correct |
69 |
Correct |
898 ms |
24440 KB |
Output is correct |
70 |
Correct |
805 ms |
23036 KB |
Output is correct |