#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
using namespace std;
typedef pair<int,int> pi;
vector<pi> graph[200005];
int ret = 1e9;
int size[200005], maxtree[200005];
int kill[200005];
int k;
vector<int> new_vis;
int dfs(int p, int pa){
if(kill[p]) return 0;
new_vis.push_back(p);
int cs = 1;
int maxv = 0;
for (int i=0; i<graph[p].size(); i++) {
if(graph[p][i].second == pa) continue;
int t = dfs(graph[p][i].second,p);
maxv = max(t,maxv);
cs += t;
}
maxtree[p] = maxv;
return size[p] = cs;
}
int get_mid(int p){
for (int i=0; i<new_vis.size(); i++) {
size[new_vis[i]] = 0;
maxtree[new_vis[i]] = 0;
}
new_vis.clear();
int full = dfs(p,-1);
int mid_pos = 0, mid_res =1e9;
for (int i=0; i<new_vis.size(); i++) {
maxtree[new_vis[i]] = max(maxtree[new_vis[i]],full - size[new_vis[i]]);
if(mid_res > maxtree[new_vis[i]]){
mid_pos = new_vis[i];
mid_res = maxtree[mid_pos];
}
}
return mid_pos;
}
int *v;
int *stamp;
int timestamp;
void solve(int p){
++timestamp;
int mid = get_mid(p);
kill[mid] = 1;
v[0] = 0;
stamp[0] = timestamp;
for (int i=0; i<graph[mid].size(); i++) {
if(kill[graph[mid][i].second]) continue;
queue<int> q,d,p,l;
vector<int> cd1, cd2;
if(graph[mid][i].first > k) continue;
q.push(graph[mid][i].second);
d.push(graph[mid][i].first);
l.push(1);
p.push(mid);
while (!q.empty()) {
int qf = q.front();
int df = d.front();
int pf = p.front();
int lf = l.front();
cd1.push_back(df);
cd2.push_back(lf);
if(stamp[k-df] == timestamp) ret = min(v[k-df] + lf,ret);
q.pop();
d.pop();
p.pop();
l.pop();
for (int i=0; i<graph[qf].size(); i++) {
int pos = graph[qf][i].second;
if(kill[pos] || pos == pf) continue;
if(df + graph[qf][i].first > k) continue;
q.push(pos);
d.push(df + graph[qf][i].first);
p.push(qf);
l.push(lf + 1);
}
}
for (int i=0; i<cd1.size(); i++) {
if(stamp[cd1[i]] != timestamp){
v[cd1[i]] = cd2[i];
stamp[cd1[i]] = timestamp;
}
else v[cd1[i]] = min(v[cd1[i]],cd2[i]);
}
}
for (int i=0; i<graph[mid].size(); i++) {
if(kill[graph[mid][i].second]) continue;
solve(graph[mid][i].second);
}
}
int best_path(int N, int K, int H[][2], int L[]){
for (int i=0; i<N-1; i++) {
graph[H[i][0]].push_back(pi(L[i],H[i][1]));
graph[H[i][1]].push_back(pi(L[i],H[i][0]));
}
v = (int*)calloc(K+1,sizeof(int));
stamp = (int*)calloc(K+1,sizeof(int));
k = K;
solve(0);
if(ret > 1e8) return -1;
return ret;
}
Compilation message
race.cpp: In function 'int dfs(int, int)':
race.cpp:25:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<graph[p].size(); i++) {
~^~~~~~~~~~~~~~~~
race.cpp: In function 'int get_mid(int)':
race.cpp:36:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<new_vis.size(); i++) {
~^~~~~~~~~~~~~~~
race.cpp:43:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<new_vis.size(); i++) {
~^~~~~~~~~~~~~~~
race.cpp: In function 'void solve(int)':
race.cpp:63:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<graph[mid].size(); i++) {
~^~~~~~~~~~~~~~~~~~
race.cpp:84:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<graph[qf].size(); i++) {
~^~~~~~~~~~~~~~~~~
race.cpp:94:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<cd1.size(); i++) {
~^~~~~~~~~~~
race.cpp:102:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<graph[mid].size(); i++) {
~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4988 KB |
Output is correct |
2 |
Correct |
7 ms |
5220 KB |
Output is correct |
3 |
Correct |
6 ms |
5220 KB |
Output is correct |
4 |
Correct |
6 ms |
5220 KB |
Output is correct |
5 |
Correct |
7 ms |
5220 KB |
Output is correct |
6 |
Correct |
6 ms |
5220 KB |
Output is correct |
7 |
Correct |
5 ms |
5220 KB |
Output is correct |
8 |
Correct |
6 ms |
5396 KB |
Output is correct |
9 |
Correct |
5 ms |
5396 KB |
Output is correct |
10 |
Correct |
7 ms |
5396 KB |
Output is correct |
11 |
Correct |
7 ms |
5396 KB |
Output is correct |
12 |
Correct |
7 ms |
5396 KB |
Output is correct |
13 |
Correct |
6 ms |
5396 KB |
Output is correct |
14 |
Correct |
8 ms |
5396 KB |
Output is correct |
15 |
Correct |
5 ms |
5396 KB |
Output is correct |
16 |
Correct |
5 ms |
5396 KB |
Output is correct |
17 |
Correct |
6 ms |
5396 KB |
Output is correct |
18 |
Correct |
7 ms |
5396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4988 KB |
Output is correct |
2 |
Correct |
7 ms |
5220 KB |
Output is correct |
3 |
Correct |
6 ms |
5220 KB |
Output is correct |
4 |
Correct |
6 ms |
5220 KB |
Output is correct |
5 |
Correct |
7 ms |
5220 KB |
Output is correct |
6 |
Correct |
6 ms |
5220 KB |
Output is correct |
7 |
Correct |
5 ms |
5220 KB |
Output is correct |
8 |
Correct |
6 ms |
5396 KB |
Output is correct |
9 |
Correct |
5 ms |
5396 KB |
Output is correct |
10 |
Correct |
7 ms |
5396 KB |
Output is correct |
11 |
Correct |
7 ms |
5396 KB |
Output is correct |
12 |
Correct |
7 ms |
5396 KB |
Output is correct |
13 |
Correct |
6 ms |
5396 KB |
Output is correct |
14 |
Correct |
8 ms |
5396 KB |
Output is correct |
15 |
Correct |
5 ms |
5396 KB |
Output is correct |
16 |
Correct |
5 ms |
5396 KB |
Output is correct |
17 |
Correct |
6 ms |
5396 KB |
Output is correct |
18 |
Correct |
7 ms |
5396 KB |
Output is correct |
19 |
Correct |
7 ms |
5396 KB |
Output is correct |
20 |
Correct |
5 ms |
5396 KB |
Output is correct |
21 |
Correct |
9 ms |
5460 KB |
Output is correct |
22 |
Correct |
11 ms |
10212 KB |
Output is correct |
23 |
Correct |
10 ms |
10212 KB |
Output is correct |
24 |
Correct |
12 ms |
11108 KB |
Output is correct |
25 |
Correct |
11 ms |
11108 KB |
Output is correct |
26 |
Correct |
9 ms |
11108 KB |
Output is correct |
27 |
Correct |
7 ms |
11108 KB |
Output is correct |
28 |
Correct |
8 ms |
11108 KB |
Output is correct |
29 |
Correct |
9 ms |
11108 KB |
Output is correct |
30 |
Correct |
9 ms |
11108 KB |
Output is correct |
31 |
Correct |
10 ms |
11108 KB |
Output is correct |
32 |
Correct |
14 ms |
11108 KB |
Output is correct |
33 |
Correct |
13 ms |
11108 KB |
Output is correct |
34 |
Correct |
12 ms |
11108 KB |
Output is correct |
35 |
Correct |
13 ms |
11220 KB |
Output is correct |
36 |
Correct |
13 ms |
11876 KB |
Output is correct |
37 |
Correct |
12 ms |
11876 KB |
Output is correct |
38 |
Correct |
9 ms |
11876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4988 KB |
Output is correct |
2 |
Correct |
7 ms |
5220 KB |
Output is correct |
3 |
Correct |
6 ms |
5220 KB |
Output is correct |
4 |
Correct |
6 ms |
5220 KB |
Output is correct |
5 |
Correct |
7 ms |
5220 KB |
Output is correct |
6 |
Correct |
6 ms |
5220 KB |
Output is correct |
7 |
Correct |
5 ms |
5220 KB |
Output is correct |
8 |
Correct |
6 ms |
5396 KB |
Output is correct |
9 |
Correct |
5 ms |
5396 KB |
Output is correct |
10 |
Correct |
7 ms |
5396 KB |
Output is correct |
11 |
Correct |
7 ms |
5396 KB |
Output is correct |
12 |
Correct |
7 ms |
5396 KB |
Output is correct |
13 |
Correct |
6 ms |
5396 KB |
Output is correct |
14 |
Correct |
8 ms |
5396 KB |
Output is correct |
15 |
Correct |
5 ms |
5396 KB |
Output is correct |
16 |
Correct |
5 ms |
5396 KB |
Output is correct |
17 |
Correct |
6 ms |
5396 KB |
Output is correct |
18 |
Correct |
7 ms |
5396 KB |
Output is correct |
19 |
Correct |
222 ms |
11876 KB |
Output is correct |
20 |
Correct |
225 ms |
11876 KB |
Output is correct |
21 |
Correct |
236 ms |
11876 KB |
Output is correct |
22 |
Correct |
226 ms |
12096 KB |
Output is correct |
23 |
Correct |
202 ms |
12096 KB |
Output is correct |
24 |
Correct |
144 ms |
12096 KB |
Output is correct |
25 |
Correct |
271 ms |
14544 KB |
Output is correct |
26 |
Correct |
191 ms |
17816 KB |
Output is correct |
27 |
Correct |
371 ms |
18448 KB |
Output is correct |
28 |
Correct |
563 ms |
29772 KB |
Output is correct |
29 |
Correct |
568 ms |
29772 KB |
Output is correct |
30 |
Correct |
361 ms |
29772 KB |
Output is correct |
31 |
Correct |
348 ms |
29772 KB |
Output is correct |
32 |
Correct |
444 ms |
29772 KB |
Output is correct |
33 |
Correct |
566 ms |
29772 KB |
Output is correct |
34 |
Correct |
454 ms |
29772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4988 KB |
Output is correct |
2 |
Correct |
7 ms |
5220 KB |
Output is correct |
3 |
Correct |
6 ms |
5220 KB |
Output is correct |
4 |
Correct |
6 ms |
5220 KB |
Output is correct |
5 |
Correct |
7 ms |
5220 KB |
Output is correct |
6 |
Correct |
6 ms |
5220 KB |
Output is correct |
7 |
Correct |
5 ms |
5220 KB |
Output is correct |
8 |
Correct |
6 ms |
5396 KB |
Output is correct |
9 |
Correct |
5 ms |
5396 KB |
Output is correct |
10 |
Correct |
7 ms |
5396 KB |
Output is correct |
11 |
Correct |
7 ms |
5396 KB |
Output is correct |
12 |
Correct |
7 ms |
5396 KB |
Output is correct |
13 |
Correct |
6 ms |
5396 KB |
Output is correct |
14 |
Correct |
8 ms |
5396 KB |
Output is correct |
15 |
Correct |
5 ms |
5396 KB |
Output is correct |
16 |
Correct |
5 ms |
5396 KB |
Output is correct |
17 |
Correct |
6 ms |
5396 KB |
Output is correct |
18 |
Correct |
7 ms |
5396 KB |
Output is correct |
19 |
Correct |
7 ms |
5396 KB |
Output is correct |
20 |
Correct |
5 ms |
5396 KB |
Output is correct |
21 |
Correct |
9 ms |
5460 KB |
Output is correct |
22 |
Correct |
11 ms |
10212 KB |
Output is correct |
23 |
Correct |
10 ms |
10212 KB |
Output is correct |
24 |
Correct |
12 ms |
11108 KB |
Output is correct |
25 |
Correct |
11 ms |
11108 KB |
Output is correct |
26 |
Correct |
9 ms |
11108 KB |
Output is correct |
27 |
Correct |
7 ms |
11108 KB |
Output is correct |
28 |
Correct |
8 ms |
11108 KB |
Output is correct |
29 |
Correct |
9 ms |
11108 KB |
Output is correct |
30 |
Correct |
9 ms |
11108 KB |
Output is correct |
31 |
Correct |
10 ms |
11108 KB |
Output is correct |
32 |
Correct |
14 ms |
11108 KB |
Output is correct |
33 |
Correct |
13 ms |
11108 KB |
Output is correct |
34 |
Correct |
12 ms |
11108 KB |
Output is correct |
35 |
Correct |
13 ms |
11220 KB |
Output is correct |
36 |
Correct |
13 ms |
11876 KB |
Output is correct |
37 |
Correct |
12 ms |
11876 KB |
Output is correct |
38 |
Correct |
9 ms |
11876 KB |
Output is correct |
39 |
Correct |
222 ms |
11876 KB |
Output is correct |
40 |
Correct |
225 ms |
11876 KB |
Output is correct |
41 |
Correct |
236 ms |
11876 KB |
Output is correct |
42 |
Correct |
226 ms |
12096 KB |
Output is correct |
43 |
Correct |
202 ms |
12096 KB |
Output is correct |
44 |
Correct |
144 ms |
12096 KB |
Output is correct |
45 |
Correct |
271 ms |
14544 KB |
Output is correct |
46 |
Correct |
191 ms |
17816 KB |
Output is correct |
47 |
Correct |
371 ms |
18448 KB |
Output is correct |
48 |
Correct |
563 ms |
29772 KB |
Output is correct |
49 |
Correct |
568 ms |
29772 KB |
Output is correct |
50 |
Correct |
361 ms |
29772 KB |
Output is correct |
51 |
Correct |
348 ms |
29772 KB |
Output is correct |
52 |
Correct |
444 ms |
29772 KB |
Output is correct |
53 |
Correct |
566 ms |
29772 KB |
Output is correct |
54 |
Correct |
454 ms |
29772 KB |
Output is correct |
55 |
Correct |
23 ms |
29772 KB |
Output is correct |
56 |
Correct |
24 ms |
29772 KB |
Output is correct |
57 |
Correct |
173 ms |
29772 KB |
Output is correct |
58 |
Correct |
103 ms |
29772 KB |
Output is correct |
59 |
Correct |
190 ms |
29772 KB |
Output is correct |
60 |
Correct |
711 ms |
34284 KB |
Output is correct |
61 |
Correct |
339 ms |
34284 KB |
Output is correct |
62 |
Correct |
462 ms |
34284 KB |
Output is correct |
63 |
Correct |
462 ms |
34284 KB |
Output is correct |
64 |
Correct |
746 ms |
34284 KB |
Output is correct |
65 |
Correct |
450 ms |
34284 KB |
Output is correct |
66 |
Correct |
1026 ms |
34460 KB |
Output is correct |
67 |
Correct |
268 ms |
34460 KB |
Output is correct |
68 |
Correct |
492 ms |
34460 KB |
Output is correct |
69 |
Correct |
522 ms |
34460 KB |
Output is correct |
70 |
Correct |
467 ms |
34460 KB |
Output is correct |