#include "race.h"
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int n,k;
int answer = 999999999;
vector<pair<int,int> > edge[200002];
queue<pair<int,int> > q,qt;
int root[1000002],small[1000002];
bool check[200002];
queue<int> qv;
int downcnt[200002];
int max_subtree[200002];
int j,nextv,tmp;
pair<int,int> top;
int make_qv(int x){
int i;
check[x] = true;
qv.push(x);
downcnt[x] = max_subtree[x] = 0;
for(i=0; i<edge[x].size(); i++){
nextv = edge[x][i].first;
if(!check[nextv]){
tmp = make_qv(nextv);
downcnt[x] += tmp;
if(max_subtree[x] < tmp) max_subtree[x] = tmp;
}
}
check[x] = false;
return (downcnt[x]+1);
}
int find_middle(int x){
int i,below_x;
int big = 0,v;
below_x = make_qv(x);
while(!qv.empty()){
v = qv.front();
tmp = max(below_x - max_subtree[v],max_subtree[v]+1);
if(big < tmp){
big = tmp;
x = v;
}
qv.pop();
}
return x;
}
void dfs(int v,int sum,int cnt){
int i;
if(sum > k) return;
check[v] = true;
q.push(make_pair(sum,cnt));
qt.push(make_pair(sum,cnt));
for(i=0; i<edge[v].size(); i++){
nextv = edge[v][i].first;
if(check[nextv]) continue;
dfs(nextv,sum+edge[v][i].second,cnt+1);
}
check[v] = false;
}
// T(n) = 2T(n/2) + O(n);
void race(int x){
int i;
int v = find_middle(x);
check[v] = true;
root[0] = v;
small[0] = 0;
for(i=0; i<edge[v].size(); i++){
nextv = edge[v][i].first;
if(check[nextv]) continue;
dfs(nextv,edge[v][i].second,1);
while(!q.empty()){
top = q.front();
if(root[k-top.first] == v) answer = min(answer,small[k-top.first] + top.second);
q.pop();
}
while(!qt.empty()){
top = qt.front();
qt.pop();
if(root[top.first] != v){
root[top.first] = v;
small[top.first] = top.second;
}else if(small[top.first] > top.second) small[top.first] = top.second;
}
}
for(i=0; i<edge[v].size(); i++){
nextv = edge[v][i].first;
if(!check[nextv]) race(nextv);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N; k = K;
for(j=0; j<n-1; j++){
edge[H[j][0]].push_back(make_pair(H[j][1],L[j]));
edge[H[j][1]].push_back(make_pair(H[j][0],L[j]));
}
for(j=0; j<=k; j++) root[j] = -1;
race(0);
if(answer == 999999999) answer = -1;
return answer;
}
Compilation message
race.cpp: In function 'int make_qv(int)':
race.cpp:26:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<edge[x].size(); i++){
~^~~~~~~~~~~~~~~
race.cpp: In function 'int find_middle(int)':
race.cpp:39:9: warning: unused variable 'i' [-Wunused-variable]
int i,below_x;
^
race.cpp: In function 'void dfs(int, int, int)':
race.cpp:64:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<edge[v].size(); i++){
~^~~~~~~~~~~~~~~
race.cpp: In function 'void race(int)':
race.cpp:82:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<edge[v].size(); i++){
~^~~~~~~~~~~~~~~
race.cpp:103:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<edge[v].size(); i++){
~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
5 ms |
5092 KB |
Output is correct |
3 |
Correct |
5 ms |
5168 KB |
Output is correct |
4 |
Correct |
5 ms |
5244 KB |
Output is correct |
5 |
Correct |
5 ms |
5320 KB |
Output is correct |
6 |
Correct |
6 ms |
5320 KB |
Output is correct |
7 |
Correct |
5 ms |
5356 KB |
Output is correct |
8 |
Correct |
5 ms |
5356 KB |
Output is correct |
9 |
Correct |
5 ms |
5356 KB |
Output is correct |
10 |
Correct |
6 ms |
5356 KB |
Output is correct |
11 |
Correct |
7 ms |
5356 KB |
Output is correct |
12 |
Correct |
6 ms |
5484 KB |
Output is correct |
13 |
Correct |
6 ms |
5484 KB |
Output is correct |
14 |
Correct |
7 ms |
5484 KB |
Output is correct |
15 |
Correct |
5 ms |
5484 KB |
Output is correct |
16 |
Correct |
6 ms |
5500 KB |
Output is correct |
17 |
Correct |
5 ms |
5500 KB |
Output is correct |
18 |
Correct |
6 ms |
5500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
5 ms |
5092 KB |
Output is correct |
3 |
Correct |
5 ms |
5168 KB |
Output is correct |
4 |
Correct |
5 ms |
5244 KB |
Output is correct |
5 |
Correct |
5 ms |
5320 KB |
Output is correct |
6 |
Correct |
6 ms |
5320 KB |
Output is correct |
7 |
Correct |
5 ms |
5356 KB |
Output is correct |
8 |
Correct |
5 ms |
5356 KB |
Output is correct |
9 |
Correct |
5 ms |
5356 KB |
Output is correct |
10 |
Correct |
6 ms |
5356 KB |
Output is correct |
11 |
Correct |
7 ms |
5356 KB |
Output is correct |
12 |
Correct |
6 ms |
5484 KB |
Output is correct |
13 |
Correct |
6 ms |
5484 KB |
Output is correct |
14 |
Correct |
7 ms |
5484 KB |
Output is correct |
15 |
Correct |
5 ms |
5484 KB |
Output is correct |
16 |
Correct |
6 ms |
5500 KB |
Output is correct |
17 |
Correct |
5 ms |
5500 KB |
Output is correct |
18 |
Correct |
6 ms |
5500 KB |
Output is correct |
19 |
Correct |
5 ms |
5500 KB |
Output is correct |
20 |
Correct |
6 ms |
5500 KB |
Output is correct |
21 |
Correct |
34 ms |
5500 KB |
Output is correct |
22 |
Correct |
20 ms |
11772 KB |
Output is correct |
23 |
Correct |
19 ms |
11772 KB |
Output is correct |
24 |
Correct |
20 ms |
12072 KB |
Output is correct |
25 |
Correct |
64 ms |
12072 KB |
Output is correct |
26 |
Correct |
17 ms |
12072 KB |
Output is correct |
27 |
Correct |
18 ms |
12072 KB |
Output is correct |
28 |
Correct |
21 ms |
12072 KB |
Output is correct |
29 |
Correct |
32 ms |
12072 KB |
Output is correct |
30 |
Correct |
34 ms |
12072 KB |
Output is correct |
31 |
Correct |
53 ms |
12072 KB |
Output is correct |
32 |
Correct |
68 ms |
12072 KB |
Output is correct |
33 |
Correct |
46 ms |
12072 KB |
Output is correct |
34 |
Correct |
20 ms |
12072 KB |
Output is correct |
35 |
Correct |
21 ms |
12072 KB |
Output is correct |
36 |
Correct |
22 ms |
12716 KB |
Output is correct |
37 |
Correct |
43 ms |
12716 KB |
Output is correct |
38 |
Correct |
46 ms |
12716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
5 ms |
5092 KB |
Output is correct |
3 |
Correct |
5 ms |
5168 KB |
Output is correct |
4 |
Correct |
5 ms |
5244 KB |
Output is correct |
5 |
Correct |
5 ms |
5320 KB |
Output is correct |
6 |
Correct |
6 ms |
5320 KB |
Output is correct |
7 |
Correct |
5 ms |
5356 KB |
Output is correct |
8 |
Correct |
5 ms |
5356 KB |
Output is correct |
9 |
Correct |
5 ms |
5356 KB |
Output is correct |
10 |
Correct |
6 ms |
5356 KB |
Output is correct |
11 |
Correct |
7 ms |
5356 KB |
Output is correct |
12 |
Correct |
6 ms |
5484 KB |
Output is correct |
13 |
Correct |
6 ms |
5484 KB |
Output is correct |
14 |
Correct |
7 ms |
5484 KB |
Output is correct |
15 |
Correct |
5 ms |
5484 KB |
Output is correct |
16 |
Correct |
6 ms |
5500 KB |
Output is correct |
17 |
Correct |
5 ms |
5500 KB |
Output is correct |
18 |
Correct |
6 ms |
5500 KB |
Output is correct |
19 |
Execution timed out |
3037 ms |
12716 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
5 ms |
5092 KB |
Output is correct |
3 |
Correct |
5 ms |
5168 KB |
Output is correct |
4 |
Correct |
5 ms |
5244 KB |
Output is correct |
5 |
Correct |
5 ms |
5320 KB |
Output is correct |
6 |
Correct |
6 ms |
5320 KB |
Output is correct |
7 |
Correct |
5 ms |
5356 KB |
Output is correct |
8 |
Correct |
5 ms |
5356 KB |
Output is correct |
9 |
Correct |
5 ms |
5356 KB |
Output is correct |
10 |
Correct |
6 ms |
5356 KB |
Output is correct |
11 |
Correct |
7 ms |
5356 KB |
Output is correct |
12 |
Correct |
6 ms |
5484 KB |
Output is correct |
13 |
Correct |
6 ms |
5484 KB |
Output is correct |
14 |
Correct |
7 ms |
5484 KB |
Output is correct |
15 |
Correct |
5 ms |
5484 KB |
Output is correct |
16 |
Correct |
6 ms |
5500 KB |
Output is correct |
17 |
Correct |
5 ms |
5500 KB |
Output is correct |
18 |
Correct |
6 ms |
5500 KB |
Output is correct |
19 |
Correct |
5 ms |
5500 KB |
Output is correct |
20 |
Correct |
6 ms |
5500 KB |
Output is correct |
21 |
Correct |
34 ms |
5500 KB |
Output is correct |
22 |
Correct |
20 ms |
11772 KB |
Output is correct |
23 |
Correct |
19 ms |
11772 KB |
Output is correct |
24 |
Correct |
20 ms |
12072 KB |
Output is correct |
25 |
Correct |
64 ms |
12072 KB |
Output is correct |
26 |
Correct |
17 ms |
12072 KB |
Output is correct |
27 |
Correct |
18 ms |
12072 KB |
Output is correct |
28 |
Correct |
21 ms |
12072 KB |
Output is correct |
29 |
Correct |
32 ms |
12072 KB |
Output is correct |
30 |
Correct |
34 ms |
12072 KB |
Output is correct |
31 |
Correct |
53 ms |
12072 KB |
Output is correct |
32 |
Correct |
68 ms |
12072 KB |
Output is correct |
33 |
Correct |
46 ms |
12072 KB |
Output is correct |
34 |
Correct |
20 ms |
12072 KB |
Output is correct |
35 |
Correct |
21 ms |
12072 KB |
Output is correct |
36 |
Correct |
22 ms |
12716 KB |
Output is correct |
37 |
Correct |
43 ms |
12716 KB |
Output is correct |
38 |
Correct |
46 ms |
12716 KB |
Output is correct |
39 |
Execution timed out |
3037 ms |
12716 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |