#include "race.h"
#include <vector>
#include <iostream>
#include <algorithm>
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
int const nmax = 200000;
int const kmax = 1000000;
struct Edge{
int to;
int cost;
};
std::vector<Edge> g[1 + nmax];
int sz[1 + nmax], seen[1 + nmax];
void dfs(int node, int parent){
sz[node] = 1;
for(int h = 0; h < g[node].size(); h++){
Edge e = g[node][h];
if(seen[e.to] == 0 && e.to != parent){
dfs(e.to, node);
sz[node] += sz[e.to];
}
}
}
int find_centroid(int node, int parent, int target){
for(int h = 0; h < g[node].size(); h++){
Edge e = g[node][h];
if(e.to != parent && seen[e.to] == 0 && target < sz[e.to])
return find_centroid(e.to, node, target);
}
return node;
}
int dp[1 + kmax];
int stepsadd = 0, stepsdelete = 0, stepsquery = 0;
void addnode(int node, int parent, int acc, int level, int &k){
if(acc <= k) {
stepsadd++;
if(level < dp[acc])
dp[acc] = level;
for(int h = 0; h < g[node].size(); h++){
Edge e = g[node][h];
if(e.to != parent && seen[e.to] == 0)
addnode(e.to, node, acc + e.cost, level + 1, k);
}
}
}
void deletenode(int node, int parent, int acc, int level, int &k){
if(acc <= k) {
stepsdelete++;
dp[acc] = nmax;
for(int h = 0; h < g[node].size(); h++){
Edge e = g[node][h];
if(e.to != parent && seen[e.to] == 0)
deletenode(e.to, node, acc + e.cost, level + 1, k);
}
}
}
void querynode(int node, int parent, int acc, int level, int &result, int &k){
if(acc <= k) {
stepsquery++;
if(dp[k - acc] + level < result)
result = dp[k - acc] + level;
for(int h = 0; h < g[node].size(); h++){
Edge e = g[node][h];
if(e.to != parent && seen[e.to] == 0)
querynode(e.to, node, acc + e.cost, level + 1, result, k);
}
}
}
void solve(int node, int &result, int &k){
dp[0] = 0;
dfs(node, -1);
int centroid = find_centroid(node, -1, sz[node] / 2);
for(int h = 0; h < g[centroid].size(); h++){
Edge e = g[centroid][h];
if(seen[e.to] == 0) {
querynode(e.to, centroid, e.cost, 1, result, k);
addnode(e.to, centroid, e.cost, 1, k);
}
}
deletenode(centroid, -1, 0, 0, k);
seen[centroid] = 1;
for(int h = 0; h < g[centroid].size(); h++){
Edge e = g[centroid][h];
if(seen[e.to] == 0)
solve(e.to, result, k);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
for(int i = 0; i <= K; i++)
dp[i] = nmax;
for(int i = 0; i < N - 1; i++){
int x = H[i][0], y = H[i][1];
g[x].push_back({y, L[i]});
g[y].push_back({x, L[i]});
}
int result = nmax, result2 = 0;
solve(0, result, K);
if(result < nmax)
return result;
else
return -1;
}
Compilation message
race.cpp: In function 'void dfs(int, int)':
race.cpp:24:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[node].size(); h++){
~~^~~~~~~~~~~~~~~~
race.cpp: In function 'int find_centroid(int, int, int)':
race.cpp:33:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[node].size(); h++){
~~^~~~~~~~~~~~~~~~
race.cpp: In function 'void addnode(int, int, int, int, int&)':
race.cpp:51:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[node].size(); h++){
~~^~~~~~~~~~~~~~~~
race.cpp: In function 'void deletenode(int, int, int, int, int&)':
race.cpp:63:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[node].size(); h++){
~~^~~~~~~~~~~~~~~~
race.cpp: In function 'void querynode(int, int, int, int, int&, int&)':
race.cpp:76:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[node].size(); h++){
~~^~~~~~~~~~~~~~~~
race.cpp: In function 'void solve(int, int&, int&)':
race.cpp:90:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[centroid].size(); h++){
~~^~~~~~~~~~~~~~~~~~~~
race.cpp:101:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[centroid].size(); h++){
~~^~~~~~~~~~~~~~~~~~~~
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:119:22: warning: unused variable 'result2' [-Wunused-variable]
int result = nmax, result2 = 0;
^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
6 ms |
4984 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
7 ms |
4984 KB |
Output is correct |
8 |
Correct |
6 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
6 ms |
4984 KB |
Output is correct |
14 |
Correct |
7 ms |
5112 KB |
Output is correct |
15 |
Correct |
6 ms |
5032 KB |
Output is correct |
16 |
Correct |
6 ms |
5112 KB |
Output is correct |
17 |
Correct |
6 ms |
5112 KB |
Output is correct |
18 |
Correct |
6 ms |
5112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
6 ms |
4984 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
7 ms |
4984 KB |
Output is correct |
8 |
Correct |
6 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
6 ms |
4984 KB |
Output is correct |
14 |
Correct |
7 ms |
5112 KB |
Output is correct |
15 |
Correct |
6 ms |
5032 KB |
Output is correct |
16 |
Correct |
6 ms |
5112 KB |
Output is correct |
17 |
Correct |
6 ms |
5112 KB |
Output is correct |
18 |
Correct |
6 ms |
5112 KB |
Output is correct |
19 |
Correct |
6 ms |
4984 KB |
Output is correct |
20 |
Correct |
6 ms |
4984 KB |
Output is correct |
21 |
Correct |
7 ms |
5112 KB |
Output is correct |
22 |
Correct |
10 ms |
8668 KB |
Output is correct |
23 |
Correct |
9 ms |
8056 KB |
Output is correct |
24 |
Correct |
9 ms |
8440 KB |
Output is correct |
25 |
Correct |
10 ms |
8440 KB |
Output is correct |
26 |
Correct |
8 ms |
6520 KB |
Output is correct |
27 |
Correct |
9 ms |
8312 KB |
Output is correct |
28 |
Correct |
8 ms |
5880 KB |
Output is correct |
29 |
Correct |
8 ms |
6392 KB |
Output is correct |
30 |
Correct |
9 ms |
6520 KB |
Output is correct |
31 |
Correct |
9 ms |
7672 KB |
Output is correct |
32 |
Correct |
9 ms |
7928 KB |
Output is correct |
33 |
Correct |
10 ms |
8316 KB |
Output is correct |
34 |
Correct |
9 ms |
7416 KB |
Output is correct |
35 |
Correct |
9 ms |
8312 KB |
Output is correct |
36 |
Correct |
10 ms |
8824 KB |
Output is correct |
37 |
Correct |
11 ms |
8176 KB |
Output is correct |
38 |
Correct |
12 ms |
7160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
6 ms |
4984 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
7 ms |
4984 KB |
Output is correct |
8 |
Correct |
6 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
6 ms |
4984 KB |
Output is correct |
14 |
Correct |
7 ms |
5112 KB |
Output is correct |
15 |
Correct |
6 ms |
5032 KB |
Output is correct |
16 |
Correct |
6 ms |
5112 KB |
Output is correct |
17 |
Correct |
6 ms |
5112 KB |
Output is correct |
18 |
Correct |
6 ms |
5112 KB |
Output is correct |
19 |
Correct |
181 ms |
12152 KB |
Output is correct |
20 |
Correct |
177 ms |
11940 KB |
Output is correct |
21 |
Correct |
245 ms |
11960 KB |
Output is correct |
22 |
Correct |
203 ms |
12220 KB |
Output is correct |
23 |
Correct |
142 ms |
12280 KB |
Output is correct |
24 |
Correct |
84 ms |
12280 KB |
Output is correct |
25 |
Correct |
201 ms |
14940 KB |
Output is correct |
26 |
Correct |
142 ms |
17784 KB |
Output is correct |
27 |
Correct |
208 ms |
19348 KB |
Output is correct |
28 |
Correct |
350 ms |
30968 KB |
Output is correct |
29 |
Correct |
324 ms |
29816 KB |
Output is correct |
30 |
Correct |
199 ms |
19468 KB |
Output is correct |
31 |
Correct |
209 ms |
19448 KB |
Output is correct |
32 |
Correct |
241 ms |
19452 KB |
Output is correct |
33 |
Correct |
298 ms |
18168 KB |
Output is correct |
34 |
Correct |
241 ms |
19064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
6 ms |
4984 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
7 ms |
4984 KB |
Output is correct |
8 |
Correct |
6 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
6 ms |
4984 KB |
Output is correct |
14 |
Correct |
7 ms |
5112 KB |
Output is correct |
15 |
Correct |
6 ms |
5032 KB |
Output is correct |
16 |
Correct |
6 ms |
5112 KB |
Output is correct |
17 |
Correct |
6 ms |
5112 KB |
Output is correct |
18 |
Correct |
6 ms |
5112 KB |
Output is correct |
19 |
Correct |
6 ms |
4984 KB |
Output is correct |
20 |
Correct |
6 ms |
4984 KB |
Output is correct |
21 |
Correct |
7 ms |
5112 KB |
Output is correct |
22 |
Correct |
10 ms |
8668 KB |
Output is correct |
23 |
Correct |
9 ms |
8056 KB |
Output is correct |
24 |
Correct |
9 ms |
8440 KB |
Output is correct |
25 |
Correct |
10 ms |
8440 KB |
Output is correct |
26 |
Correct |
8 ms |
6520 KB |
Output is correct |
27 |
Correct |
9 ms |
8312 KB |
Output is correct |
28 |
Correct |
8 ms |
5880 KB |
Output is correct |
29 |
Correct |
8 ms |
6392 KB |
Output is correct |
30 |
Correct |
9 ms |
6520 KB |
Output is correct |
31 |
Correct |
9 ms |
7672 KB |
Output is correct |
32 |
Correct |
9 ms |
7928 KB |
Output is correct |
33 |
Correct |
10 ms |
8316 KB |
Output is correct |
34 |
Correct |
9 ms |
7416 KB |
Output is correct |
35 |
Correct |
9 ms |
8312 KB |
Output is correct |
36 |
Correct |
10 ms |
8824 KB |
Output is correct |
37 |
Correct |
11 ms |
8176 KB |
Output is correct |
38 |
Correct |
12 ms |
7160 KB |
Output is correct |
39 |
Correct |
181 ms |
12152 KB |
Output is correct |
40 |
Correct |
177 ms |
11940 KB |
Output is correct |
41 |
Correct |
245 ms |
11960 KB |
Output is correct |
42 |
Correct |
203 ms |
12220 KB |
Output is correct |
43 |
Correct |
142 ms |
12280 KB |
Output is correct |
44 |
Correct |
84 ms |
12280 KB |
Output is correct |
45 |
Correct |
201 ms |
14940 KB |
Output is correct |
46 |
Correct |
142 ms |
17784 KB |
Output is correct |
47 |
Correct |
208 ms |
19348 KB |
Output is correct |
48 |
Correct |
350 ms |
30968 KB |
Output is correct |
49 |
Correct |
324 ms |
29816 KB |
Output is correct |
50 |
Correct |
199 ms |
19468 KB |
Output is correct |
51 |
Correct |
209 ms |
19448 KB |
Output is correct |
52 |
Correct |
241 ms |
19452 KB |
Output is correct |
53 |
Correct |
298 ms |
18168 KB |
Output is correct |
54 |
Correct |
241 ms |
19064 KB |
Output is correct |
55 |
Correct |
18 ms |
5752 KB |
Output is correct |
56 |
Correct |
20 ms |
5752 KB |
Output is correct |
57 |
Correct |
114 ms |
12252 KB |
Output is correct |
58 |
Correct |
48 ms |
12012 KB |
Output is correct |
59 |
Correct |
148 ms |
18528 KB |
Output is correct |
60 |
Correct |
606 ms |
34040 KB |
Output is correct |
61 |
Correct |
250 ms |
19676 KB |
Output is correct |
62 |
Correct |
256 ms |
23516 KB |
Output is correct |
63 |
Correct |
332 ms |
23544 KB |
Output is correct |
64 |
Correct |
687 ms |
21812 KB |
Output is correct |
65 |
Correct |
261 ms |
20344 KB |
Output is correct |
66 |
Correct |
524 ms |
31992 KB |
Output is correct |
67 |
Correct |
134 ms |
24040 KB |
Output is correct |
68 |
Correct |
316 ms |
24056 KB |
Output is correct |
69 |
Correct |
311 ms |
24312 KB |
Output is correct |
70 |
Correct |
303 ms |
23800 KB |
Output is correct |