#include "race.h"
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
// 문제 : frozen 과 비슷
int i,n,k;
int answer = 999999999;
vector<pair<int,int> > edge[200002];
queue<pair<int,int> > q;
int root[1000002],small[1000002];
bool check[200002],checkv[200002];
vector<int> down[200002];
queue<int> qv;
int downcnt[200002];
void make_down(int v){
int i,sizes,nextv;
qv.push(v);
checkv[v] = true;
while(!qv.empty()){
v = qv.front();
qv.pop();
sizes = edge[v].size();
for(i=0; i<sizes; i++){
nextv = edge[v][i].first;
if(!checkv[nextv]){
checkv[nextv] = true;
qv.push(nextv);
down[v].push_back(nextv);
}
}
}
}
int find_sum(int x){
int i,sum = 1;
int sizes,tmp;
sizes = down[x].size();
for(i=0; i<sizes; i++){
tmp = find_sum(down[x][i]);
sum += tmp;
}
downcnt[x] = (sum-1);
return sum;
}
int find_middle(int x){
int i,sizes,nextv;
int tmp;
while(1){
sizes = down[x].size();
for(i=0; i<sizes; i++){
nextv = down[x][i];
if(!check[nextv] && downcnt[nextv]+1 > (downcnt[x]+2)/2) break;
}
if(i == sizes) break;
tmp = downcnt[x]+1;
downcnt[x] -= (downcnt[nextv]+1);
downcnt[nextv] += tmp;
down[nextv].push_back(x);
x = nextv;
}
return x;
}
void dfs(int v,long long sum,int cnt){
int i,sizes;
int nextv;
if(sum > k) return;
check[v] = true;
q.push(make_pair(sum,cnt));
sizes = edge[v].size();
for(i=0; i<sizes; i++){
nextv = edge[v][i].first;
if(check[nextv]) continue;
dfs(nextv,sum+edge[v][i].second,cnt+1);
}
check[v] = false;
}
void race(int x){
int i,sizes;
int nextv;
int tsum,tcnt;
int v = find_middle(x);
check[v] = true;
sizes = edge[v].size();
root[0] = v;
small[0] = 0;
for(i=0; i<sizes; i++){
nextv = edge[v][i].first;
if(check[nextv]) continue;
dfs(nextv,edge[v][i].second,1);
while(!q.empty()){
tsum = q.front().first;
if(root[k-tsum] == v) answer = min(answer,small[k-tsum] + q.front().second);
q.pop();
}
dfs(nextv,edge[v][i].second,1);
while(!q.empty()){
tsum = q.front().first;
tcnt = q.front().second;
q.pop();
if(root[tsum] != v){
root[tsum] = v;
small[tsum] = tcnt;
}else if(small[tsum] > tcnt) small[tsum] = tcnt;
}
}
for(i=0; i<sizes; i++){
nextv = edge[v][i].first;
if(!check[nextv]) race(nextv);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
int i;
n = N; k = K;
for(i=0; i<n-1; i++){
edge[H[i][0]].push_back(make_pair(H[i][1],L[i]));
edge[H[i][1]].push_back(make_pair(H[i][0],L[i]));
}
for(i=0; i<=k; i++) root[i] = -1;
make_down(0);
find_sum(0);
race(0);
if(answer == 999999999) answer = -1;
return answer;
}
Compilation message
race.cpp: In function 'int find_middle(int)':
race.cpp:53:17: warning: 'nextv' may be used uninitialized in this function [-Wmaybe-uninitialized]
int i,sizes,nextv;
^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
9720 KB |
Output is correct |
2 |
Correct |
9 ms |
9828 KB |
Output is correct |
3 |
Correct |
10 ms |
9904 KB |
Output is correct |
4 |
Correct |
14 ms |
9904 KB |
Output is correct |
5 |
Correct |
11 ms |
9904 KB |
Output is correct |
6 |
Correct |
9 ms |
9980 KB |
Output is correct |
7 |
Correct |
10 ms |
9980 KB |
Output is correct |
8 |
Correct |
11 ms |
9980 KB |
Output is correct |
9 |
Correct |
11 ms |
10080 KB |
Output is correct |
10 |
Correct |
11 ms |
10080 KB |
Output is correct |
11 |
Correct |
10 ms |
10080 KB |
Output is correct |
12 |
Correct |
10 ms |
10080 KB |
Output is correct |
13 |
Correct |
12 ms |
10080 KB |
Output is correct |
14 |
Correct |
10 ms |
10080 KB |
Output is correct |
15 |
Correct |
10 ms |
10080 KB |
Output is correct |
16 |
Correct |
12 ms |
10080 KB |
Output is correct |
17 |
Correct |
9 ms |
10080 KB |
Output is correct |
18 |
Correct |
9 ms |
10080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
9720 KB |
Output is correct |
2 |
Correct |
9 ms |
9828 KB |
Output is correct |
3 |
Correct |
10 ms |
9904 KB |
Output is correct |
4 |
Correct |
14 ms |
9904 KB |
Output is correct |
5 |
Correct |
11 ms |
9904 KB |
Output is correct |
6 |
Correct |
9 ms |
9980 KB |
Output is correct |
7 |
Correct |
10 ms |
9980 KB |
Output is correct |
8 |
Correct |
11 ms |
9980 KB |
Output is correct |
9 |
Correct |
11 ms |
10080 KB |
Output is correct |
10 |
Correct |
11 ms |
10080 KB |
Output is correct |
11 |
Correct |
10 ms |
10080 KB |
Output is correct |
12 |
Correct |
10 ms |
10080 KB |
Output is correct |
13 |
Correct |
12 ms |
10080 KB |
Output is correct |
14 |
Correct |
10 ms |
10080 KB |
Output is correct |
15 |
Correct |
10 ms |
10080 KB |
Output is correct |
16 |
Correct |
12 ms |
10080 KB |
Output is correct |
17 |
Correct |
9 ms |
10080 KB |
Output is correct |
18 |
Correct |
9 ms |
10080 KB |
Output is correct |
19 |
Correct |
10 ms |
10080 KB |
Output is correct |
20 |
Correct |
11 ms |
10080 KB |
Output is correct |
21 |
Correct |
11 ms |
10108 KB |
Output is correct |
22 |
Correct |
15 ms |
16252 KB |
Output is correct |
23 |
Correct |
15 ms |
16252 KB |
Output is correct |
24 |
Correct |
17 ms |
16252 KB |
Output is correct |
25 |
Correct |
17 ms |
16252 KB |
Output is correct |
26 |
Correct |
18 ms |
16252 KB |
Output is correct |
27 |
Correct |
13 ms |
16252 KB |
Output is correct |
28 |
Correct |
14 ms |
16252 KB |
Output is correct |
29 |
Correct |
13 ms |
16252 KB |
Output is correct |
30 |
Correct |
14 ms |
16252 KB |
Output is correct |
31 |
Correct |
16 ms |
16252 KB |
Output is correct |
32 |
Correct |
17 ms |
16252 KB |
Output is correct |
33 |
Correct |
16 ms |
16252 KB |
Output is correct |
34 |
Correct |
15 ms |
16252 KB |
Output is correct |
35 |
Correct |
22 ms |
16468 KB |
Output is correct |
36 |
Correct |
18 ms |
17404 KB |
Output is correct |
37 |
Correct |
20 ms |
17404 KB |
Output is correct |
38 |
Correct |
16 ms |
17404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
9720 KB |
Output is correct |
2 |
Correct |
9 ms |
9828 KB |
Output is correct |
3 |
Correct |
10 ms |
9904 KB |
Output is correct |
4 |
Correct |
14 ms |
9904 KB |
Output is correct |
5 |
Correct |
11 ms |
9904 KB |
Output is correct |
6 |
Correct |
9 ms |
9980 KB |
Output is correct |
7 |
Correct |
10 ms |
9980 KB |
Output is correct |
8 |
Correct |
11 ms |
9980 KB |
Output is correct |
9 |
Correct |
11 ms |
10080 KB |
Output is correct |
10 |
Correct |
11 ms |
10080 KB |
Output is correct |
11 |
Correct |
10 ms |
10080 KB |
Output is correct |
12 |
Correct |
10 ms |
10080 KB |
Output is correct |
13 |
Correct |
12 ms |
10080 KB |
Output is correct |
14 |
Correct |
10 ms |
10080 KB |
Output is correct |
15 |
Correct |
10 ms |
10080 KB |
Output is correct |
16 |
Correct |
12 ms |
10080 KB |
Output is correct |
17 |
Correct |
9 ms |
10080 KB |
Output is correct |
18 |
Correct |
9 ms |
10080 KB |
Output is correct |
19 |
Correct |
254 ms |
17468 KB |
Output is correct |
20 |
Correct |
229 ms |
17468 KB |
Output is correct |
21 |
Correct |
270 ms |
17468 KB |
Output is correct |
22 |
Correct |
264 ms |
17788 KB |
Output is correct |
23 |
Correct |
135 ms |
17788 KB |
Output is correct |
24 |
Correct |
106 ms |
17788 KB |
Output is correct |
25 |
Execution timed out |
3040 ms |
20576 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
9720 KB |
Output is correct |
2 |
Correct |
9 ms |
9828 KB |
Output is correct |
3 |
Correct |
10 ms |
9904 KB |
Output is correct |
4 |
Correct |
14 ms |
9904 KB |
Output is correct |
5 |
Correct |
11 ms |
9904 KB |
Output is correct |
6 |
Correct |
9 ms |
9980 KB |
Output is correct |
7 |
Correct |
10 ms |
9980 KB |
Output is correct |
8 |
Correct |
11 ms |
9980 KB |
Output is correct |
9 |
Correct |
11 ms |
10080 KB |
Output is correct |
10 |
Correct |
11 ms |
10080 KB |
Output is correct |
11 |
Correct |
10 ms |
10080 KB |
Output is correct |
12 |
Correct |
10 ms |
10080 KB |
Output is correct |
13 |
Correct |
12 ms |
10080 KB |
Output is correct |
14 |
Correct |
10 ms |
10080 KB |
Output is correct |
15 |
Correct |
10 ms |
10080 KB |
Output is correct |
16 |
Correct |
12 ms |
10080 KB |
Output is correct |
17 |
Correct |
9 ms |
10080 KB |
Output is correct |
18 |
Correct |
9 ms |
10080 KB |
Output is correct |
19 |
Correct |
10 ms |
10080 KB |
Output is correct |
20 |
Correct |
11 ms |
10080 KB |
Output is correct |
21 |
Correct |
11 ms |
10108 KB |
Output is correct |
22 |
Correct |
15 ms |
16252 KB |
Output is correct |
23 |
Correct |
15 ms |
16252 KB |
Output is correct |
24 |
Correct |
17 ms |
16252 KB |
Output is correct |
25 |
Correct |
17 ms |
16252 KB |
Output is correct |
26 |
Correct |
18 ms |
16252 KB |
Output is correct |
27 |
Correct |
13 ms |
16252 KB |
Output is correct |
28 |
Correct |
14 ms |
16252 KB |
Output is correct |
29 |
Correct |
13 ms |
16252 KB |
Output is correct |
30 |
Correct |
14 ms |
16252 KB |
Output is correct |
31 |
Correct |
16 ms |
16252 KB |
Output is correct |
32 |
Correct |
17 ms |
16252 KB |
Output is correct |
33 |
Correct |
16 ms |
16252 KB |
Output is correct |
34 |
Correct |
15 ms |
16252 KB |
Output is correct |
35 |
Correct |
22 ms |
16468 KB |
Output is correct |
36 |
Correct |
18 ms |
17404 KB |
Output is correct |
37 |
Correct |
20 ms |
17404 KB |
Output is correct |
38 |
Correct |
16 ms |
17404 KB |
Output is correct |
39 |
Correct |
254 ms |
17468 KB |
Output is correct |
40 |
Correct |
229 ms |
17468 KB |
Output is correct |
41 |
Correct |
270 ms |
17468 KB |
Output is correct |
42 |
Correct |
264 ms |
17788 KB |
Output is correct |
43 |
Correct |
135 ms |
17788 KB |
Output is correct |
44 |
Correct |
106 ms |
17788 KB |
Output is correct |
45 |
Execution timed out |
3040 ms |
20576 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |