# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1076632 |
2024-08-26T15:09:43 Z |
rayan_bd |
Race (IOI11_race) |
C++17 |
|
0 ms |
0 KB |
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
const int mxN = 2e5+10;
const ll INF = (int)1e9;
vector<pair<ll,ll>> adj[mxN];
ll sz[mxN],mnDist[(int)1e7],N,K,ans=INF;
bool ok[mxN];
void dfs1(ll node,ll par){
sz[node]=0;
for(auto it:adj[node]){
if(it.first==par) continue;
dfs1(it.first,node);
sz[node]+=sz[it.first];
}
sz[node]++;
}
ll find(ll node,ll par){
for(auto it:adj[node]){
if(it.first==par) continue;
if(sz[it.first]>=((N+1)/2)){
return find(it.first,node);
}
}
return node;
}
void dfs2(ll src,ll par,ll dist,ll h,bool f){
if(dist>K) return;
if(f){
mnDist[dist]=min(mnDist[dist],h);
}else{
ans=min(ans,mnDist[K-dist]+h);
}
for(auto it:adj[src]){
if(!ok[it.first]&&it.first!=par){
dfs2(it.first,src,dist+it.second,h+1,f);
}
}
}
void res(ll u,ll p,ll dist){
if(dist>K) return;
mnDist[dist]=INF;
for(auto it:adj[u]){
if(it.first!=p&&!ok[it.first]){
res(it.first,u,dist+it.second);
}
}
}
void decompose(ll u){
dfs1(u,-1);
ll cen=find(u,-1);
mnDist[0]=0;
for(auto it:adj[u]){
if(!ok[it.first]){
dfs2(it.first,u,it.second,1,0);
dfs2(it.first,u,it.second,1,1);
}
}
res(u,-1,0);
ok[u]=1;
for(auto it:adj[u]){
if(!ok[it.first]){
decompose(it.first);
}
}
}
int best_path(int n,int k,int h[][2],int l[]){
memset(mnDist,0x3f,sizeof(mnDist));
for(ll i=0;i<n-1;++i){
adj[h[i][0]].pb({h[i][1],l[i]});
adj[h[i][1]].pb({h[i][0],l[i]});
}
N=n,K=k;
decompose(0);
return ans==INF?-1:ans;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
#endif
ll n,k;cin>>n>>k;
ll h[n-1][2],l[n-1];
for(ll i=0;i<n-1;++i){
cin>>h[i][0]>>h[i][1];
}
for(ll i=0;i<n-1;++i){
cin>>l[i];
}
cout<<best_path(n,k,h,l);
return 0;
}
Compilation message
race.cpp: In function 'void decompose(long long int)':
race.cpp:61:8: warning: unused variable 'cen' [-Wunused-variable]
61 | ll cen=find(u,-1);
| ^~~
race.cpp: In function 'int main()':
race.cpp:104:25: error: cannot convert 'long long int (*)[2]' to 'int (*)[2]'
104 | cout<<best_path(n,k,h,l);
| ^
| |
| long long int (*)[2]
race.cpp:78:31: note: initializing argument 3 of 'int best_path(int, int, int (*)[2], int*)'
78 | int best_path(int n,int k,int h[][2],int l[]){
| ~~~~^~~~~~
race.cpp:91:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | freopen("input.in","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
race.cpp:92:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen("output.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~