#include "race.h"
#include <bits/stdc++.h>
typedef long long ll;
#define FOR(i,x,y) for(ll i=x; i<y; i++)
#define FORNEG(i,x,y) for(ll i=x; i>y; i--)
using namespace std;
const ll maxn = 200005;
const ll INF = 1000000000;
vector<basic_string<ll>> edges[maxn];
ll par[maxn];
ll centpar[maxn];
ll depth[maxn];
ll realdepth[maxn];
ll ancc[maxn][20];
bool visited[maxn];
ll sub[maxn];
vector<array<ll,2>> idkman;
ll ans = INF;
ll KK;
ll sus[1000005];
ll anc(ll a, ll x){
if (a==0) return 0;
if (x==0) return par[a];
if (ancc[a][x] != -1) return ancc[a][x];
return ancc[a][x] = anc(anc(a,x-1),x-1);
}
ll lca(ll a, ll b){
if (depth[a] < depth[b]) swap(a,b);
FORNEG(i,19,-1) if (depth[anc(a, i)] >= depth[b]) a = anc(a,i);
if (a==b) return a;
FORNEG(i,19,-1) if (anc(a,i) != anc(b,i)) a = anc(a,i), b = anc(b,i);
return par[a];
}
void initdfs(ll a,ll p, ll d, ll dd){
par[a] = p;
depth[a] = d;
realdepth[a] = dd;
for (auto&i : edges[a]){
if (i[0] != p) initdfs(i[0], a, d+1, dd+i[1]);
}
}
ll centroid(ll root, ll a, ll p){
for (auto&i : edges[a]){
if (!visited[i[0]] && i[0] != p && sub[i[0]] > sub[root]/2) return centroid(root, i[0], a);
}
return a;
}
void dfs(ll a, ll p){
sub[a] = 1;
for (auto&i : edges[a]){
if(i[0] != p && !visited[i[0]]) dfs(i[0], a), sub[a] += sub[i[0]];
}
}
void dfs2(ll a, ll p, ll d, ll dd){
for (auto&i : edges[a]){
if (i[0] != p && !visited[i[0]]) dfs2(i[0], a, d+1,dd + i[1]);
}
idkman.push_back({d, dd});
}
void build(ll a, ll p){
dfs(a,p);
ll cent = centroid(a, a, p);
centpar[cent] = p;
visited[cent] = 1;
basic_string<ll> seen;
sus[0] = 0;
for (auto&i : edges[cent]){
if (visited[i[0]]) continue;
idkman.clear();
dfs2(i[0], cent, 1, i[1]);
seen.push_back(0);
sus[0] = 0;
for (auto&k : idkman){
ll req = KK - k[1];
if (0<=req && req <= KK) ans = min(ans, k[0] + sus[req]);
}
for (auto&k : idkman){
seen.push_back(k[1]);
if (0<=k[1] && k[1] <= KK) sus[k[1]] = min(sus[k[1]], k[0]);
}
}
for (auto&k : seen) if (0<=k && k<=KK) sus[k] = INF;
for (auto&i : edges[cent]){
if (!visited[i[0]]) build(i[0], cent);
}
}
vector<array<ll,3>> dists[maxn];
int best_path(int N, int K, int H[][2], int L[])
{
KK = K;
FOR(i,0,maxn) par[i] = -1, centpar[i] = -1, par[i] -1, depth[i] = -1, realdepth[i] = -1,visited[i] = 0,dists[i].clear(),edges[i].clear(), sus[i] = INF;
FOR(i,0,maxn)FOR(j,0,20) ancc[i][j] = -1;
FOR(i,0,1000005) sus[i] = INF;
FOR(i,0,N-1){
edges[H[i][0]].push_back({H[i][1], L[i]});
edges[H[i][1]].push_back({H[i][0], L[i]});
}
initdfs(0,-1,0,0);
dfs(0,-1);
build(0,-1);
if (ans >= INF) return -1;
else return ans;
}
Compilation message
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:112:56: warning: right operand of comma operator has no effect [-Wunused-value]
112 | FOR(i,0,maxn) par[i] = -1, centpar[i] = -1, par[i] -1, depth[i] = -1, realdepth[i] = -1,visited[i] = 0,dists[i].clear(),edges[i].clear(), sus[i] = INF;
| ~~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
60504 KB |
Output is correct |
2 |
Correct |
7 ms |
60508 KB |
Output is correct |
3 |
Correct |
7 ms |
60580 KB |
Output is correct |
4 |
Correct |
7 ms |
60508 KB |
Output is correct |
5 |
Correct |
7 ms |
60572 KB |
Output is correct |
6 |
Correct |
7 ms |
60628 KB |
Output is correct |
7 |
Correct |
7 ms |
60508 KB |
Output is correct |
8 |
Correct |
7 ms |
60508 KB |
Output is correct |
9 |
Correct |
7 ms |
60508 KB |
Output is correct |
10 |
Correct |
7 ms |
60508 KB |
Output is correct |
11 |
Correct |
7 ms |
60664 KB |
Output is correct |
12 |
Correct |
7 ms |
60756 KB |
Output is correct |
13 |
Correct |
6 ms |
60508 KB |
Output is correct |
14 |
Correct |
7 ms |
60504 KB |
Output is correct |
15 |
Correct |
6 ms |
60508 KB |
Output is correct |
16 |
Correct |
7 ms |
60508 KB |
Output is correct |
17 |
Correct |
7 ms |
60608 KB |
Output is correct |
18 |
Correct |
7 ms |
60508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
60504 KB |
Output is correct |
2 |
Correct |
7 ms |
60508 KB |
Output is correct |
3 |
Correct |
7 ms |
60580 KB |
Output is correct |
4 |
Correct |
7 ms |
60508 KB |
Output is correct |
5 |
Correct |
7 ms |
60572 KB |
Output is correct |
6 |
Correct |
7 ms |
60628 KB |
Output is correct |
7 |
Correct |
7 ms |
60508 KB |
Output is correct |
8 |
Correct |
7 ms |
60508 KB |
Output is correct |
9 |
Correct |
7 ms |
60508 KB |
Output is correct |
10 |
Correct |
7 ms |
60508 KB |
Output is correct |
11 |
Correct |
7 ms |
60664 KB |
Output is correct |
12 |
Correct |
7 ms |
60756 KB |
Output is correct |
13 |
Correct |
6 ms |
60508 KB |
Output is correct |
14 |
Correct |
7 ms |
60504 KB |
Output is correct |
15 |
Correct |
6 ms |
60508 KB |
Output is correct |
16 |
Correct |
7 ms |
60508 KB |
Output is correct |
17 |
Correct |
7 ms |
60608 KB |
Output is correct |
18 |
Correct |
7 ms |
60508 KB |
Output is correct |
19 |
Correct |
7 ms |
60508 KB |
Output is correct |
20 |
Correct |
7 ms |
60508 KB |
Output is correct |
21 |
Correct |
7 ms |
60580 KB |
Output is correct |
22 |
Correct |
7 ms |
60620 KB |
Output is correct |
23 |
Correct |
8 ms |
60776 KB |
Output is correct |
24 |
Correct |
7 ms |
60612 KB |
Output is correct |
25 |
Correct |
7 ms |
60764 KB |
Output is correct |
26 |
Correct |
7 ms |
60764 KB |
Output is correct |
27 |
Correct |
7 ms |
60796 KB |
Output is correct |
28 |
Correct |
8 ms |
60760 KB |
Output is correct |
29 |
Correct |
7 ms |
60764 KB |
Output is correct |
30 |
Correct |
7 ms |
60764 KB |
Output is correct |
31 |
Correct |
7 ms |
60764 KB |
Output is correct |
32 |
Correct |
7 ms |
60764 KB |
Output is correct |
33 |
Correct |
7 ms |
60760 KB |
Output is correct |
34 |
Correct |
8 ms |
60764 KB |
Output is correct |
35 |
Correct |
7 ms |
60620 KB |
Output is correct |
36 |
Correct |
7 ms |
60760 KB |
Output is correct |
37 |
Correct |
7 ms |
60764 KB |
Output is correct |
38 |
Correct |
7 ms |
60628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
60504 KB |
Output is correct |
2 |
Correct |
7 ms |
60508 KB |
Output is correct |
3 |
Correct |
7 ms |
60580 KB |
Output is correct |
4 |
Correct |
7 ms |
60508 KB |
Output is correct |
5 |
Correct |
7 ms |
60572 KB |
Output is correct |
6 |
Correct |
7 ms |
60628 KB |
Output is correct |
7 |
Correct |
7 ms |
60508 KB |
Output is correct |
8 |
Correct |
7 ms |
60508 KB |
Output is correct |
9 |
Correct |
7 ms |
60508 KB |
Output is correct |
10 |
Correct |
7 ms |
60508 KB |
Output is correct |
11 |
Correct |
7 ms |
60664 KB |
Output is correct |
12 |
Correct |
7 ms |
60756 KB |
Output is correct |
13 |
Correct |
6 ms |
60508 KB |
Output is correct |
14 |
Correct |
7 ms |
60504 KB |
Output is correct |
15 |
Correct |
6 ms |
60508 KB |
Output is correct |
16 |
Correct |
7 ms |
60508 KB |
Output is correct |
17 |
Correct |
7 ms |
60608 KB |
Output is correct |
18 |
Correct |
7 ms |
60508 KB |
Output is correct |
19 |
Correct |
118 ms |
80180 KB |
Output is correct |
20 |
Correct |
123 ms |
80044 KB |
Output is correct |
21 |
Correct |
112 ms |
80068 KB |
Output is correct |
22 |
Correct |
99 ms |
80276 KB |
Output is correct |
23 |
Correct |
101 ms |
80560 KB |
Output is correct |
24 |
Correct |
55 ms |
78416 KB |
Output is correct |
25 |
Correct |
123 ms |
84092 KB |
Output is correct |
26 |
Correct |
78 ms |
85576 KB |
Output is correct |
27 |
Correct |
218 ms |
94680 KB |
Output is correct |
28 |
Correct |
270 ms |
108356 KB |
Output is correct |
29 |
Correct |
266 ms |
107400 KB |
Output is correct |
30 |
Correct |
183 ms |
94680 KB |
Output is correct |
31 |
Correct |
195 ms |
94684 KB |
Output is correct |
32 |
Correct |
241 ms |
94408 KB |
Output is correct |
33 |
Correct |
199 ms |
96308 KB |
Output is correct |
34 |
Correct |
206 ms |
95536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
60504 KB |
Output is correct |
2 |
Correct |
7 ms |
60508 KB |
Output is correct |
3 |
Correct |
7 ms |
60580 KB |
Output is correct |
4 |
Correct |
7 ms |
60508 KB |
Output is correct |
5 |
Correct |
7 ms |
60572 KB |
Output is correct |
6 |
Correct |
7 ms |
60628 KB |
Output is correct |
7 |
Correct |
7 ms |
60508 KB |
Output is correct |
8 |
Correct |
7 ms |
60508 KB |
Output is correct |
9 |
Correct |
7 ms |
60508 KB |
Output is correct |
10 |
Correct |
7 ms |
60508 KB |
Output is correct |
11 |
Correct |
7 ms |
60664 KB |
Output is correct |
12 |
Correct |
7 ms |
60756 KB |
Output is correct |
13 |
Correct |
6 ms |
60508 KB |
Output is correct |
14 |
Correct |
7 ms |
60504 KB |
Output is correct |
15 |
Correct |
6 ms |
60508 KB |
Output is correct |
16 |
Correct |
7 ms |
60508 KB |
Output is correct |
17 |
Correct |
7 ms |
60608 KB |
Output is correct |
18 |
Correct |
7 ms |
60508 KB |
Output is correct |
19 |
Correct |
7 ms |
60508 KB |
Output is correct |
20 |
Correct |
7 ms |
60508 KB |
Output is correct |
21 |
Correct |
7 ms |
60580 KB |
Output is correct |
22 |
Correct |
7 ms |
60620 KB |
Output is correct |
23 |
Correct |
8 ms |
60776 KB |
Output is correct |
24 |
Correct |
7 ms |
60612 KB |
Output is correct |
25 |
Correct |
7 ms |
60764 KB |
Output is correct |
26 |
Correct |
7 ms |
60764 KB |
Output is correct |
27 |
Correct |
7 ms |
60796 KB |
Output is correct |
28 |
Correct |
8 ms |
60760 KB |
Output is correct |
29 |
Correct |
7 ms |
60764 KB |
Output is correct |
30 |
Correct |
7 ms |
60764 KB |
Output is correct |
31 |
Correct |
7 ms |
60764 KB |
Output is correct |
32 |
Correct |
7 ms |
60764 KB |
Output is correct |
33 |
Correct |
7 ms |
60760 KB |
Output is correct |
34 |
Correct |
8 ms |
60764 KB |
Output is correct |
35 |
Correct |
7 ms |
60620 KB |
Output is correct |
36 |
Correct |
7 ms |
60760 KB |
Output is correct |
37 |
Correct |
7 ms |
60764 KB |
Output is correct |
38 |
Correct |
7 ms |
60628 KB |
Output is correct |
39 |
Correct |
118 ms |
80180 KB |
Output is correct |
40 |
Correct |
123 ms |
80044 KB |
Output is correct |
41 |
Correct |
112 ms |
80068 KB |
Output is correct |
42 |
Correct |
99 ms |
80276 KB |
Output is correct |
43 |
Correct |
101 ms |
80560 KB |
Output is correct |
44 |
Correct |
55 ms |
78416 KB |
Output is correct |
45 |
Correct |
123 ms |
84092 KB |
Output is correct |
46 |
Correct |
78 ms |
85576 KB |
Output is correct |
47 |
Correct |
218 ms |
94680 KB |
Output is correct |
48 |
Correct |
270 ms |
108356 KB |
Output is correct |
49 |
Correct |
266 ms |
107400 KB |
Output is correct |
50 |
Correct |
183 ms |
94680 KB |
Output is correct |
51 |
Correct |
195 ms |
94684 KB |
Output is correct |
52 |
Correct |
241 ms |
94408 KB |
Output is correct |
53 |
Correct |
199 ms |
96308 KB |
Output is correct |
54 |
Correct |
206 ms |
95536 KB |
Output is correct |
55 |
Correct |
14 ms |
62300 KB |
Output is correct |
56 |
Correct |
13 ms |
62376 KB |
Output is correct |
57 |
Correct |
62 ms |
80552 KB |
Output is correct |
58 |
Correct |
39 ms |
81344 KB |
Output is correct |
59 |
Correct |
78 ms |
85452 KB |
Output is correct |
60 |
Correct |
268 ms |
107844 KB |
Output is correct |
61 |
Correct |
204 ms |
94684 KB |
Output is correct |
62 |
Correct |
190 ms |
94404 KB |
Output is correct |
63 |
Correct |
238 ms |
94408 KB |
Output is correct |
64 |
Correct |
356 ms |
97260 KB |
Output is correct |
65 |
Correct |
304 ms |
97560 KB |
Output is correct |
66 |
Correct |
295 ms |
105384 KB |
Output is correct |
67 |
Correct |
93 ms |
98200 KB |
Output is correct |
68 |
Correct |
216 ms |
96020 KB |
Output is correct |
69 |
Correct |
224 ms |
96556 KB |
Output is correct |
70 |
Correct |
217 ms |
94768 KB |
Output is correct |