#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],maxnum[200002],maxsum[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;
if(maxsum[x] < tmp){
maxsum[x] = tmp;
maxnum[x] = down[x][i];
}
}
downcnt[x] = (sum-1);
return sum;
}
int find_middle(int x){
int v,tmp;
while(1){
v = maxnum[x];
if(v == -1 || downcnt[v]+1 <= (downcnt[x]+1)/2) break;
tmp = downcnt[x]+1;
downcnt[x] -= (downcnt[v]+1);
downcnt[v] += tmp;
maxsum[x] = 0;
maxnum[x] = -1;
for(int i=0; i<down[x].size(); i++){
if(down[x][i] != v && maxsum[x] < downcnt[down[x][i]]+1){
maxsum[x] = downcnt[down[x][i]]+1;
maxnum[x] = down[x][i];
}
}
down[v].push_back(x);
x = v;
}
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;
tcnt = q.front().second;
q.pop();
if(root[k-tsum] == v) answer = min(answer,small[k-tsum] + tcnt);
}
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;
for(i=0; i<n; i++) maxnum[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:67:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<down[x].size(); i++){
~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
9720 KB |
Output is correct |
2 |
Correct |
8 ms |
9828 KB |
Output is correct |
3 |
Correct |
9 ms |
9828 KB |
Output is correct |
4 |
Correct |
11 ms |
9904 KB |
Output is correct |
5 |
Correct |
9 ms |
9904 KB |
Output is correct |
6 |
Correct |
10 ms |
9904 KB |
Output is correct |
7 |
Correct |
9 ms |
9904 KB |
Output is correct |
8 |
Correct |
10 ms |
9956 KB |
Output is correct |
9 |
Correct |
10 ms |
9968 KB |
Output is correct |
10 |
Correct |
13 ms |
9968 KB |
Output is correct |
11 |
Correct |
10 ms |
9968 KB |
Output is correct |
12 |
Correct |
10 ms |
9968 KB |
Output is correct |
13 |
Correct |
11 ms |
9968 KB |
Output is correct |
14 |
Correct |
10 ms |
9968 KB |
Output is correct |
15 |
Correct |
10 ms |
10044 KB |
Output is correct |
16 |
Correct |
10 ms |
10044 KB |
Output is correct |
17 |
Correct |
10 ms |
10044 KB |
Output is correct |
18 |
Correct |
9 ms |
10044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
9720 KB |
Output is correct |
2 |
Correct |
8 ms |
9828 KB |
Output is correct |
3 |
Correct |
9 ms |
9828 KB |
Output is correct |
4 |
Correct |
11 ms |
9904 KB |
Output is correct |
5 |
Correct |
9 ms |
9904 KB |
Output is correct |
6 |
Correct |
10 ms |
9904 KB |
Output is correct |
7 |
Correct |
9 ms |
9904 KB |
Output is correct |
8 |
Correct |
10 ms |
9956 KB |
Output is correct |
9 |
Correct |
10 ms |
9968 KB |
Output is correct |
10 |
Correct |
13 ms |
9968 KB |
Output is correct |
11 |
Correct |
10 ms |
9968 KB |
Output is correct |
12 |
Correct |
10 ms |
9968 KB |
Output is correct |
13 |
Correct |
11 ms |
9968 KB |
Output is correct |
14 |
Correct |
10 ms |
9968 KB |
Output is correct |
15 |
Correct |
10 ms |
10044 KB |
Output is correct |
16 |
Correct |
10 ms |
10044 KB |
Output is correct |
17 |
Correct |
10 ms |
10044 KB |
Output is correct |
18 |
Correct |
9 ms |
10044 KB |
Output is correct |
19 |
Correct |
10 ms |
10044 KB |
Output is correct |
20 |
Correct |
10 ms |
10064 KB |
Output is correct |
21 |
Correct |
10 ms |
10108 KB |
Output is correct |
22 |
Correct |
16 ms |
16252 KB |
Output is correct |
23 |
Correct |
14 ms |
16252 KB |
Output is correct |
24 |
Correct |
16 ms |
16252 KB |
Output is correct |
25 |
Correct |
17 ms |
16252 KB |
Output is correct |
26 |
Correct |
13 ms |
16252 KB |
Output is correct |
27 |
Correct |
16 ms |
16252 KB |
Output is correct |
28 |
Incorrect |
14 ms |
16252 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
9720 KB |
Output is correct |
2 |
Correct |
8 ms |
9828 KB |
Output is correct |
3 |
Correct |
9 ms |
9828 KB |
Output is correct |
4 |
Correct |
11 ms |
9904 KB |
Output is correct |
5 |
Correct |
9 ms |
9904 KB |
Output is correct |
6 |
Correct |
10 ms |
9904 KB |
Output is correct |
7 |
Correct |
9 ms |
9904 KB |
Output is correct |
8 |
Correct |
10 ms |
9956 KB |
Output is correct |
9 |
Correct |
10 ms |
9968 KB |
Output is correct |
10 |
Correct |
13 ms |
9968 KB |
Output is correct |
11 |
Correct |
10 ms |
9968 KB |
Output is correct |
12 |
Correct |
10 ms |
9968 KB |
Output is correct |
13 |
Correct |
11 ms |
9968 KB |
Output is correct |
14 |
Correct |
10 ms |
9968 KB |
Output is correct |
15 |
Correct |
10 ms |
10044 KB |
Output is correct |
16 |
Correct |
10 ms |
10044 KB |
Output is correct |
17 |
Correct |
10 ms |
10044 KB |
Output is correct |
18 |
Correct |
9 ms |
10044 KB |
Output is correct |
19 |
Correct |
550 ms |
19336 KB |
Output is correct |
20 |
Correct |
599 ms |
19336 KB |
Output is correct |
21 |
Correct |
962 ms |
19336 KB |
Output is correct |
22 |
Correct |
767 ms |
19336 KB |
Output is correct |
23 |
Correct |
141 ms |
19336 KB |
Output is correct |
24 |
Correct |
104 ms |
19336 KB |
Output is correct |
25 |
Execution timed out |
3052 ms |
21244 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
9720 KB |
Output is correct |
2 |
Correct |
8 ms |
9828 KB |
Output is correct |
3 |
Correct |
9 ms |
9828 KB |
Output is correct |
4 |
Correct |
11 ms |
9904 KB |
Output is correct |
5 |
Correct |
9 ms |
9904 KB |
Output is correct |
6 |
Correct |
10 ms |
9904 KB |
Output is correct |
7 |
Correct |
9 ms |
9904 KB |
Output is correct |
8 |
Correct |
10 ms |
9956 KB |
Output is correct |
9 |
Correct |
10 ms |
9968 KB |
Output is correct |
10 |
Correct |
13 ms |
9968 KB |
Output is correct |
11 |
Correct |
10 ms |
9968 KB |
Output is correct |
12 |
Correct |
10 ms |
9968 KB |
Output is correct |
13 |
Correct |
11 ms |
9968 KB |
Output is correct |
14 |
Correct |
10 ms |
9968 KB |
Output is correct |
15 |
Correct |
10 ms |
10044 KB |
Output is correct |
16 |
Correct |
10 ms |
10044 KB |
Output is correct |
17 |
Correct |
10 ms |
10044 KB |
Output is correct |
18 |
Correct |
9 ms |
10044 KB |
Output is correct |
19 |
Correct |
10 ms |
10044 KB |
Output is correct |
20 |
Correct |
10 ms |
10064 KB |
Output is correct |
21 |
Correct |
10 ms |
10108 KB |
Output is correct |
22 |
Correct |
16 ms |
16252 KB |
Output is correct |
23 |
Correct |
14 ms |
16252 KB |
Output is correct |
24 |
Correct |
16 ms |
16252 KB |
Output is correct |
25 |
Correct |
17 ms |
16252 KB |
Output is correct |
26 |
Correct |
13 ms |
16252 KB |
Output is correct |
27 |
Correct |
16 ms |
16252 KB |
Output is correct |
28 |
Incorrect |
14 ms |
16252 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |