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 <bits/stdc++.h>
using namespace std;
typedef pair <int,int> pii;
int N,K,L[200003];
int par[200003],sz[200003];
vector <pii> adj[200003];
int check[1000003];
bool del[200003];
int ans = 1987654321;
vector <pii> lazy;
int dfs1(int now,int prev)
{
bool reaf=true;
for(int i=0;i<adj[now].size();i++)
{
int next = adj[now][i].first;
int cost = adj[now][i].second;
if(next == prev) continue;
if(del[next]) continue;
reaf = false;
sz[now] += dfs1(next,now);
}
return ++sz[now];
}
int find_cen(int now,int prev)
{
for(int i=0;i<adj[now].size();i++)
{
int next = adj[now][i].first;
int cost = adj[now][i].second;
if(next == prev) continue;
if(del[next]) continue;
if(sz[next]*2>sz[now]) return find_cen(next,now);
}
return now;
}
int dfs2(int now,int prev,int sum,int depth)
{
int ret = 1987654321;
if(sum>K) return 1987654321;
if(check[K-sum]!=-1)
ret= min(ret,depth + check[K-sum]);
lazy.push_back(pii(sum,depth));
for(int i=0;i<adj[now].size();i++)
{
int next = adj[now][i].first;
int cost = adj[now][i].second;
if(next == prev) continue;
if(del[next]) continue;
ret = min(ret,dfs2(next,now,sum+cost,depth+1));
if(sum==0)
{
for(pii &it:lazy) check[it.first] = it.second;
lazy.clear();
}
}
return ret;
}
void g(int now,int prev)
{
dfs1(now,-1);
int cen = find_cen(now,-1);
memset(check,-1,sizeof(check));
check[now] = 0;
ans = min(ans,dfs2(cen,-1,0,0));
del[cen] = true;
for(int i=0;i<adj[cen].size();i++)
{
int next = adj[cen][i].first;
int cost = adj[cen][i].second;
if(next == prev) continue;
if(del[next]) continue;
g(next,now);
}
}
int best_path(int n, int k, int h[][2], int l[])
{
N= n ; K = k;
for(int i=0;i<n-1;i++)
{
adj[h[i][0]].push_back(pii(h[i][1],l[i]));
adj[h[i][1]].push_back(pii(h[i][0],l[i]));
}
g(1,-1);
return (ans>=1987654300)?-1:ans;
}
Compilation message (stderr)
race.cpp: In function 'int dfs1(int, int)':
race.cpp:15:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<adj[now].size();i++)
~^~~~~~~~~~~~~~~~
race.cpp:18:13: warning: unused variable 'cost' [-Wunused-variable]
int cost = adj[now][i].second;
^~~~
race.cpp:14:10: warning: variable 'reaf' set but not used [-Wunused-but-set-variable]
bool reaf=true;
^~~~
race.cpp: In function 'int find_cen(int, int)':
race.cpp:29:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<adj[now].size();i++)
~^~~~~~~~~~~~~~~~
race.cpp:32:13: warning: unused variable 'cost' [-Wunused-variable]
int cost = adj[now][i].second;
^~~~
race.cpp: In function 'int dfs2(int, int, int, int)':
race.cpp:47:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<adj[now].size();i++)
~^~~~~~~~~~~~~~~~
race.cpp: In function 'void g(int, int)':
race.cpp:71:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<adj[cen].size();i++)
~^~~~~~~~~~~~~~~~
race.cpp:74:13: warning: unused variable 'cost' [-Wunused-variable]
int cost = adj[cen][i].second;
^~~~
# | 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... |