#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define debug(x) cerr << #x << " " << x << "\n"
#define debug_with_space(x) cerr << #x << " " << x << " "
#include"race.h"
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <long double, pii> muchie;
typedef tree <ll, null_type, less_equal <ll>, rb_tree_tag, tree_order_statistics_node_update> OST;
const ll NMAX = 200001;
const ll INF = (1LL << 60);
const ll MOD = 1000000009;
const ll BLOCK = 225;
const ll base = 31;
const ll base2 = 53;
const ll nr_of_bits = 21;
vector <pii> v[NMAX];
int best = 2e9;
int k;
int sz[NMAX];
int total;
int viz[NMAX];
int d[NMAX];
struct ura{
int first, pfirst, second, psecond;
};
ura mp[1000001];
void getsize(int node, int p){
sz[node] = 1;
for(auto x : v[node]){
if(x.first == p || viz[x.first])
continue;
getsize(x.first, node);
sz[node] += sz[x.first];
}
total = sz[node];
}
int centroid(int node, int p){
for(auto x : v[node]){
if(viz[x.first] || x.first == p){
continue;
}
if(sz[x.first] > total / 2)
return centroid(x.first, node);
}
return node;
}
int OneCentroid(int node){
getsize(node, 0);
return centroid(node, 0);
}
void ComputeDist(int node, int p, int subTree, int dist, int level){
d[node] = dist;
pii act = {level, subTree};
if(dist <= k && (mp[dist].pfirst == -1 || mp[dist].first >= level)){
if(mp[dist].pfirst != subTree){
swap(mp[dist].pfirst, mp[dist].psecond);
swap(mp[dist].first, mp[dist].second);
}
mp[dist].first = level;
mp[dist].pfirst = subTree;
}else if(dist <= k && mp[dist].pfirst != subTree && (mp[dist].psecond == -1 || mp[dist].second >= level)){
mp[dist].second = level;
mp[dist].psecond = subTree;
}
for(auto x : v[node]){
if(x.first == p || viz[x.first])
continue;
ComputeDist(x.first, node, subTree, d[node] + x.second, level + 1);
}
}
void Erase(int node, int p, int subTree, int level){
int dist = d[node];
pii act = {level, subTree};
if(dist <= k && (mp[dist].pfirst != -1 && mp[dist].pfirst == subTree)){
swap(mp[dist].pfirst, mp[dist].psecond);
swap(mp[dist].first, mp[dist].second);
mp[dist].psecond = -1;
mp[dist].second = 2e9;
}
for(auto x : v[node]){
if(x.first == p || viz[x.first])
continue;
Erase(x.first, node, subTree, level + 1);
}
}
void SuperErase(int node, int p, int subTree, int level){
int dist = d[node];
pii act = {level, subTree};
if(dist <= k){
mp[dist] = {(int)2e9, -1, (int)2e9, -1};
}
for(auto x : v[node]){
if(x.first == p || viz[x.first])
continue;
SuperErase(x.first, node, subTree, level + 1);
}
}
void Add(int node, int p, int subTree, int level){
int dist = d[node];
pii act = {level, subTree};
if(dist <= k && (mp[dist].pfirst == -1 || mp[dist].first >= level)){
if(mp[dist].pfirst != subTree){
swap(mp[dist].pfirst, mp[dist].psecond);
swap(mp[dist].first, mp[dist].second);
}
mp[dist].first = level;
mp[dist].pfirst = subTree;
}else if(dist <= k && mp[dist].pfirst != subTree && (mp[dist].psecond == -1 || mp[dist].second >= level)){
mp[dist].second = level;
mp[dist].psecond = subTree;
}
for(auto x : v[node]){
if(x.first == p || viz[x.first])
continue;
Add(x.first, node, subTree, level + 1);
}
}
void Compute(int node, int p, int level){
int trb = k - d[node];
if(trb >= 0 && mp[trb].pfirst != -1)
{
int b = mp[trb].first;
best = min(best, b + level);
}
for(auto x : v[node]){
if(x.first == p || viz[x.first])
continue;
Compute(x.first, node, level + 1);
}
}
void CD(int node){
int c = OneCentroid(node);
viz[c] = 1;
mp[0].first = 0;
mp[0].pfirst = 0;
for(auto x : v[c]){
if(viz[x.first])
continue;
ComputeDist(x.first, c, x.first, x.second, 1);
}
for(auto x : v[c]){
if(viz[x.first])
continue;
Erase(x.first, c, x.first, 1);
Compute(x.first, 0, 1);
Add(x.first, c, x.first, 1);
}
for(auto x : v[c]){
if(viz[x.first])
continue;
SuperErase(x.first, c, x.first, 1);
}
for(auto x : v[c]){
if(viz[x.first])
continue;
CD(x.first);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
k = K;
for(int i = 0; i < N - 1; i++){
int a = H[i][0] + 1;
int b = H[i][1] + 1;
v[a].push_back({b, L[i]});
v[b].push_back({a, L[i]});
}
for(int i = 0; i <= k; i++)
mp[i] = {(int)2e9, -1, (int)2e9, -1};
CD(1);
if(best == 2e9)
best = -1;
return best;
}
Compilation message
race.cpp: In function 'void ComputeDist(int, int, int, int, int)':
race.cpp:65:9: warning: variable 'act' set but not used [-Wunused-but-set-variable]
65 | pii act = {level, subTree};
| ^~~
race.cpp: In function 'void Erase(int, int, int, int)':
race.cpp:86:9: warning: variable 'act' set but not used [-Wunused-but-set-variable]
86 | pii act = {level, subTree};
| ^~~
race.cpp: In function 'void SuperErase(int, int, int, int)':
race.cpp:102:9: warning: variable 'act' set but not used [-Wunused-but-set-variable]
102 | pii act = {level, subTree};
| ^~~
race.cpp: In function 'void Add(int, int, int, int)':
race.cpp:116:9: warning: variable 'act' set but not used [-Wunused-but-set-variable]
116 | pii act = {level, subTree};
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4984 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 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 |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 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 |
4 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 |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4984 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 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 |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 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 |
4 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 |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
12 ms |
19420 KB |
Output is correct |
23 |
Correct |
12 ms |
16796 KB |
Output is correct |
24 |
Correct |
12 ms |
18640 KB |
Output is correct |
25 |
Correct |
12 ms |
18280 KB |
Output is correct |
26 |
Correct |
8 ms |
10400 KB |
Output is correct |
27 |
Correct |
12 ms |
17584 KB |
Output is correct |
28 |
Correct |
7 ms |
8216 KB |
Output is correct |
29 |
Correct |
8 ms |
10188 KB |
Output is correct |
30 |
Correct |
8 ms |
10848 KB |
Output is correct |
31 |
Correct |
11 ms |
15308 KB |
Output is correct |
32 |
Correct |
11 ms |
16204 KB |
Output is correct |
33 |
Correct |
12 ms |
17228 KB |
Output is correct |
34 |
Correct |
10 ms |
14284 KB |
Output is correct |
35 |
Correct |
13 ms |
17756 KB |
Output is correct |
36 |
Correct |
13 ms |
19660 KB |
Output is correct |
37 |
Correct |
12 ms |
17484 KB |
Output is correct |
38 |
Correct |
9 ms |
13004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4984 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 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 |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 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 |
4 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 |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
297 ms |
12444 KB |
Output is correct |
20 |
Correct |
307 ms |
12484 KB |
Output is correct |
21 |
Correct |
302 ms |
12544 KB |
Output is correct |
22 |
Correct |
269 ms |
12576 KB |
Output is correct |
23 |
Correct |
197 ms |
12908 KB |
Output is correct |
24 |
Correct |
98 ms |
12040 KB |
Output is correct |
25 |
Correct |
266 ms |
16068 KB |
Output is correct |
26 |
Correct |
146 ms |
19780 KB |
Output is correct |
27 |
Correct |
282 ms |
20424 KB |
Output is correct |
28 |
Correct |
844 ms |
34884 KB |
Output is correct |
29 |
Correct |
800 ms |
33528 KB |
Output is correct |
30 |
Correct |
291 ms |
20420 KB |
Output is correct |
31 |
Correct |
299 ms |
20464 KB |
Output is correct |
32 |
Correct |
402 ms |
20324 KB |
Output is correct |
33 |
Correct |
538 ms |
19504 KB |
Output is correct |
34 |
Correct |
514 ms |
19092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4984 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 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 |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 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 |
4 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 |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
12 ms |
19420 KB |
Output is correct |
23 |
Correct |
12 ms |
16796 KB |
Output is correct |
24 |
Correct |
12 ms |
18640 KB |
Output is correct |
25 |
Correct |
12 ms |
18280 KB |
Output is correct |
26 |
Correct |
8 ms |
10400 KB |
Output is correct |
27 |
Correct |
12 ms |
17584 KB |
Output is correct |
28 |
Correct |
7 ms |
8216 KB |
Output is correct |
29 |
Correct |
8 ms |
10188 KB |
Output is correct |
30 |
Correct |
8 ms |
10848 KB |
Output is correct |
31 |
Correct |
11 ms |
15308 KB |
Output is correct |
32 |
Correct |
11 ms |
16204 KB |
Output is correct |
33 |
Correct |
12 ms |
17228 KB |
Output is correct |
34 |
Correct |
10 ms |
14284 KB |
Output is correct |
35 |
Correct |
13 ms |
17756 KB |
Output is correct |
36 |
Correct |
13 ms |
19660 KB |
Output is correct |
37 |
Correct |
12 ms |
17484 KB |
Output is correct |
38 |
Correct |
9 ms |
13004 KB |
Output is correct |
39 |
Correct |
297 ms |
12444 KB |
Output is correct |
40 |
Correct |
307 ms |
12484 KB |
Output is correct |
41 |
Correct |
302 ms |
12544 KB |
Output is correct |
42 |
Correct |
269 ms |
12576 KB |
Output is correct |
43 |
Correct |
197 ms |
12908 KB |
Output is correct |
44 |
Correct |
98 ms |
12040 KB |
Output is correct |
45 |
Correct |
266 ms |
16068 KB |
Output is correct |
46 |
Correct |
146 ms |
19780 KB |
Output is correct |
47 |
Correct |
282 ms |
20424 KB |
Output is correct |
48 |
Correct |
844 ms |
34884 KB |
Output is correct |
49 |
Correct |
800 ms |
33528 KB |
Output is correct |
50 |
Correct |
291 ms |
20420 KB |
Output is correct |
51 |
Correct |
299 ms |
20464 KB |
Output is correct |
52 |
Correct |
402 ms |
20324 KB |
Output is correct |
53 |
Correct |
538 ms |
19504 KB |
Output is correct |
54 |
Correct |
514 ms |
19092 KB |
Output is correct |
55 |
Correct |
17 ms |
5836 KB |
Output is correct |
56 |
Correct |
19 ms |
5776 KB |
Output is correct |
57 |
Correct |
138 ms |
12748 KB |
Output is correct |
58 |
Correct |
44 ms |
12068 KB |
Output is correct |
59 |
Correct |
169 ms |
22724 KB |
Output is correct |
60 |
Correct |
960 ms |
52536 KB |
Output is correct |
61 |
Correct |
295 ms |
23580 KB |
Output is correct |
62 |
Correct |
313 ms |
39012 KB |
Output is correct |
63 |
Correct |
450 ms |
39196 KB |
Output is correct |
64 |
Correct |
1089 ms |
31388 KB |
Output is correct |
65 |
Correct |
700 ms |
23840 KB |
Output is correct |
66 |
Correct |
1000 ms |
49836 KB |
Output is correct |
67 |
Correct |
155 ms |
38056 KB |
Output is correct |
68 |
Correct |
496 ms |
38980 KB |
Output is correct |
69 |
Correct |
490 ms |
39084 KB |
Output is correct |
70 |
Correct |
453 ms |
38152 KB |
Output is correct |