#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define ll long long
#define ull unsigned ll
#include "race.h"
using namespace std;
constexpr int debug = 1;
constexpr int M = 2e5+7;
constexpr int D = 1e6+7;
int tree_size[M];
int total_size;
bool was_centroid[M];
ll Kek;
ll mini = 1e9+7;
ll closest[D];
queue<int> changes; //komorki z closest do wyzerowania
vector<pair<ll,ll>> adj[M];
inline void addEdge(ll a, ll b, ll val){
adj[a].pb({b, val});
adj[b].pb({a, val});
}
void set_size(int s, int p){
tree_size[s] = 1;
for(auto k: adj[s]){
if(k.F == p || was_centroid[k.F]) continue;
set_size(k.F, s);
tree_size[s] += tree_size[k.F];
}
}
int find_centroid(int s, int p){
for(auto k: adj[s]){
if(was_centroid[k.F]) continue;
else if(k.F == p){
if(total_size-tree_size[s] > total_size/2) return find_centroid(k.F, s);
}
else if(tree_size[k.F] > total_size/2){
return find_centroid(k.F, s);
}
}
return s;
}
void DFS1(int s, int p, ll dist_v, ll dist_e){ //sprawdza, ile ma odpowiadajacy wierzcholek
if(dist_e == Kek) mini = min(mini, dist_v);
if((dist_e < Kek) && (closest[Kek-dist_e])) mini = min(mini, closest[Kek-dist_e]+dist_v);
for(auto k: adj[s]){
if(was_centroid[k.F] || k.F == p) continue;
DFS1(k.F, s, dist_v+1, dist_e+k.S);
}
}
void DFS2(int s, int p, ll dist_v, ll dist_e){ //aktualizacje wierzcholkow w danych odleglosciach
if(dist_e <= Kek && dist_e != 0){
if(closest[dist_e] == 0 || closest[dist_e] > dist_v){
changes.push(dist_e);
closest[dist_e] = dist_v;
}
}
for(auto k: adj[s]){
if(was_centroid[k.F] || k.F == p) continue;
DFS2(k.F, s, dist_v+1, dist_e+k.S);
}
}
void centroid_decomposition(int x){
set_size(x, x);
total_size = tree_size[x];
int cen = find_centroid(x, x);
//robi cos na centroidzie
was_centroid[cen] = 1;
for(auto k: adj[cen]){
if(!was_centroid[k.F]){
DFS1(k.F, cen, 1, k.S);
DFS2(k.F, cen, 1, k.S);
}
}
while(!changes.empty()){
closest[changes.front()] = 0;
changes.pop();
}
for(auto k: adj[cen]){
if(!was_centroid[k.F]) centroid_decomposition(k.F);
}
}
int best_path(int N, int K, int H[][2], int L[]){
Kek = K;
for(int i = 0; i < N-1; i++){
addEdge(H[i][0], H[i][1], L[i]);
}
centroid_decomposition(0);
if(mini == 1e9+7) mini = -1;
return mini;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
3 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
2 ms |
12636 KB |
Output is correct |
17 |
Correct |
1 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
3 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
2 ms |
12636 KB |
Output is correct |
17 |
Correct |
1 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 ms |
12636 KB |
Output is correct |
20 |
Correct |
2 ms |
12636 KB |
Output is correct |
21 |
Correct |
2 ms |
12752 KB |
Output is correct |
22 |
Correct |
3 ms |
14872 KB |
Output is correct |
23 |
Correct |
3 ms |
14804 KB |
Output is correct |
24 |
Correct |
3 ms |
15452 KB |
Output is correct |
25 |
Correct |
2 ms |
13916 KB |
Output is correct |
26 |
Correct |
3 ms |
13404 KB |
Output is correct |
27 |
Correct |
2 ms |
12636 KB |
Output is correct |
28 |
Correct |
2 ms |
12888 KB |
Output is correct |
29 |
Correct |
3 ms |
13148 KB |
Output is correct |
30 |
Correct |
3 ms |
13404 KB |
Output is correct |
31 |
Correct |
3 ms |
14008 KB |
Output is correct |
32 |
Correct |
3 ms |
14008 KB |
Output is correct |
33 |
Correct |
4 ms |
15448 KB |
Output is correct |
34 |
Correct |
3 ms |
14940 KB |
Output is correct |
35 |
Correct |
3 ms |
15708 KB |
Output is correct |
36 |
Correct |
3 ms |
15452 KB |
Output is correct |
37 |
Correct |
2 ms |
14172 KB |
Output is correct |
38 |
Correct |
2 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
3 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
2 ms |
12636 KB |
Output is correct |
17 |
Correct |
1 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
90 ms |
20308 KB |
Output is correct |
20 |
Correct |
86 ms |
21340 KB |
Output is correct |
21 |
Correct |
82 ms |
21076 KB |
Output is correct |
22 |
Correct |
75 ms |
21704 KB |
Output is correct |
23 |
Correct |
64 ms |
22108 KB |
Output is correct |
24 |
Correct |
40 ms |
21084 KB |
Output is correct |
25 |
Correct |
95 ms |
25884 KB |
Output is correct |
26 |
Correct |
62 ms |
28500 KB |
Output is correct |
27 |
Correct |
106 ms |
27984 KB |
Output is correct |
28 |
Correct |
236 ms |
42068 KB |
Output is correct |
29 |
Correct |
221 ms |
41296 KB |
Output is correct |
30 |
Correct |
104 ms |
28428 KB |
Output is correct |
31 |
Correct |
108 ms |
28500 KB |
Output is correct |
32 |
Correct |
114 ms |
28500 KB |
Output is correct |
33 |
Correct |
154 ms |
27216 KB |
Output is correct |
34 |
Correct |
168 ms |
27784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
3 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
2 ms |
12636 KB |
Output is correct |
17 |
Correct |
1 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 ms |
12636 KB |
Output is correct |
20 |
Correct |
2 ms |
12636 KB |
Output is correct |
21 |
Correct |
2 ms |
12752 KB |
Output is correct |
22 |
Correct |
3 ms |
14872 KB |
Output is correct |
23 |
Correct |
3 ms |
14804 KB |
Output is correct |
24 |
Correct |
3 ms |
15452 KB |
Output is correct |
25 |
Correct |
2 ms |
13916 KB |
Output is correct |
26 |
Correct |
3 ms |
13404 KB |
Output is correct |
27 |
Correct |
2 ms |
12636 KB |
Output is correct |
28 |
Correct |
2 ms |
12888 KB |
Output is correct |
29 |
Correct |
3 ms |
13148 KB |
Output is correct |
30 |
Correct |
3 ms |
13404 KB |
Output is correct |
31 |
Correct |
3 ms |
14008 KB |
Output is correct |
32 |
Correct |
3 ms |
14008 KB |
Output is correct |
33 |
Correct |
4 ms |
15448 KB |
Output is correct |
34 |
Correct |
3 ms |
14940 KB |
Output is correct |
35 |
Correct |
3 ms |
15708 KB |
Output is correct |
36 |
Correct |
3 ms |
15452 KB |
Output is correct |
37 |
Correct |
2 ms |
14172 KB |
Output is correct |
38 |
Correct |
2 ms |
12892 KB |
Output is correct |
39 |
Correct |
90 ms |
20308 KB |
Output is correct |
40 |
Correct |
86 ms |
21340 KB |
Output is correct |
41 |
Correct |
82 ms |
21076 KB |
Output is correct |
42 |
Correct |
75 ms |
21704 KB |
Output is correct |
43 |
Correct |
64 ms |
22108 KB |
Output is correct |
44 |
Correct |
40 ms |
21084 KB |
Output is correct |
45 |
Correct |
95 ms |
25884 KB |
Output is correct |
46 |
Correct |
62 ms |
28500 KB |
Output is correct |
47 |
Correct |
106 ms |
27984 KB |
Output is correct |
48 |
Correct |
236 ms |
42068 KB |
Output is correct |
49 |
Correct |
221 ms |
41296 KB |
Output is correct |
50 |
Correct |
104 ms |
28428 KB |
Output is correct |
51 |
Correct |
108 ms |
28500 KB |
Output is correct |
52 |
Correct |
114 ms |
28500 KB |
Output is correct |
53 |
Correct |
154 ms |
27216 KB |
Output is correct |
54 |
Correct |
168 ms |
27784 KB |
Output is correct |
55 |
Correct |
8 ms |
13148 KB |
Output is correct |
56 |
Correct |
7 ms |
13304 KB |
Output is correct |
57 |
Correct |
46 ms |
20828 KB |
Output is correct |
58 |
Correct |
24 ms |
21000 KB |
Output is correct |
59 |
Correct |
65 ms |
29008 KB |
Output is correct |
60 |
Correct |
245 ms |
45292 KB |
Output is correct |
61 |
Correct |
109 ms |
27732 KB |
Output is correct |
62 |
Correct |
112 ms |
28536 KB |
Output is correct |
63 |
Correct |
119 ms |
28500 KB |
Output is correct |
64 |
Correct |
217 ms |
27488 KB |
Output is correct |
65 |
Correct |
141 ms |
29032 KB |
Output is correct |
66 |
Correct |
215 ms |
40944 KB |
Output is correct |
67 |
Correct |
62 ms |
28348 KB |
Output is correct |
68 |
Correct |
117 ms |
29964 KB |
Output is correct |
69 |
Correct |
115 ms |
27988 KB |
Output is correct |
70 |
Correct |
107 ms |
29576 KB |
Output is correct |