This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "race.h"
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
// 문제 : frozen 과 비슷
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],checkv[200002];
vector<int> down[200002];
queue<int> qv;
int downcnt[200002];
pair<int,int> top;
void make_down(int v){
int i,nextv;
qv.push(v);
checkv[v] = true;
while(!qv.empty()){
v = qv.front();
qv.pop();
for(i=0; i<edge[v].size(); 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;
for(i=0; i<down[x].size(); i++){
sum += find_sum(down[x][i]);
}
downcnt[x] = (sum-1);
return sum;
}
int find_middle(int x){
int i,nextv;
while(1){
for(i=0; i<down[x].size(); i++){
nextv = down[x][i];
if(!check[nextv] && downcnt[nextv]+1 > (downcnt[x]+1)/2) break;
}
if(i == down[x].size()) break;
downcnt[x] -= (downcnt[nextv]+1);
down[nextv].push_back(x);
x = nextv;
}
return x;
}
void dfs(int v,int sum,int cnt){
int i,nextv;
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,nextv;
int tsum,tcnt;
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[])
{
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 (stderr)
race.cpp: In function 'void make_down(int)':
race.cpp:28:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<edge[v].size(); i++){
~^~~~~~~~~~~~~~~
race.cpp: In function 'int find_sum(int)':
race.cpp:42:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<down[x].size(); i++){
~^~~~~~~~~~~~~~~
race.cpp: In function 'int find_middle(int)':
race.cpp:53:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<down[x].size(); i++){
~^~~~~~~~~~~~~~~
race.cpp:58:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i == down[x].size()) break;
~~^~~~~~~~~~~~~~~~~
race.cpp: In function 'void dfs(int, int, int)':
race.cpp:75: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:94:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<edge[v].size(); i++){
~^~~~~~~~~~~~~~~
race.cpp:115:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<edge[v].size(); i++){
~^~~~~~~~~~~~~~~
race.cpp:87:9: warning: unused variable 'tsum' [-Wunused-variable]
int tsum,tcnt;
^~~~
race.cpp:87:14: warning: unused variable 'tcnt' [-Wunused-variable]
int tsum,tcnt;
^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |