# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
542713 | AmirElarbi | Race (IOI11_race) | C++14 | 0 ms | 0 KiB |
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 <bits/stdc++.h>
#define vi vector<int>
#define ve vector
#define ll long long
#define vf vector<float>
#define vll vector<pair<ll,ll>>
#define ii pair<int,int>
#define pll pair<ll,ll>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 2e9+5
#define eps 1e-7
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
#define V 450
#define MAX_N 500000
using namespace std;
const int MOD = 1e9+7;
//#include "race.h"
const int nax = 2e5+5;
const int nax2 = 1e6+5;
vii adj[nax];
bool vis[nax];
int N, K;
int H[MAX_N][2];
int L[MAX_N];
int solution;
int sb[nax], cnt[nax2], ans = INF;
int dfs(int u, int p){
sb[u] = 1;
for(auto x : adj[u]){
if(x.fi == p || vis[x.fi]) continue;
sb[u] += dfs(x.fi,u);
}
return sb[u];
}
int getc(int u, int p, int ms){
for(auto x : adj[u]){
if(x.fi == p || vis[x.fi]) continue;
if(sb[x.fi]*2 > ms)
return getc(x.fi,u,ms);
}
return u;
}
void update(int u, int p, bool feel, int dep, int dist){
if(dist > K) return;
if(feel) cnt[dist] = min(cnt[dist],dep);
else ans = min(ans, dep+cnt[K-dist]);
for(auto x : adj[u]){
if(x.fi == p || vis[x.fi]) continue;
update(x.fi,u,feel,dep+1,dist+x.se);
}
}
void del(int u, int p, int dist){
if(dist > K) return;
cnt[dist] = INF;
for(auto x : adj[u]){
if(x.fi == p || vis[x.fi]) continue;
del(x.fi,u,dist+x.se);
}
}
void centdec(int u, int p){
int c = getc(u,u,dfs(u,u));
vis[c] = 1;
for(auto x : adj[c]){
if(vis[x.fi]) continue;
update(x.fi,u,0,1,x.se);
update(x.fi,u,1,1,x.se);
}
for(auto x : adj[c]){
if(vis[x.fi]) continue;
del(x.fi,c,x.se);
}
for(auto x : adj[c]){
if(vis[x.fi]) continue;
centdec(x.fi,c);
}
}
int best_path(int n, int k, int h[][2], int s[])
{
for (int i = 0; i < n-1; ++i)
{
adj[h[i][0]].pb({h[i][1],s[i]});
adj[h[i][1]].pb({h[i][0],s[i]});
}
for (int i = 0; i <= k; ++i)
{
cnt[i] = INF;
}
centdec(0,-1);
if(ans == INF) return -1;
else return ans;
}
void read_input()
{
int i;
scanf("%d %d",&N,&K);
for(i=0; i<N-1; i++)
scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]);
scanf("%d",&solution);
}
int main()
{
optimise;
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int ans;
read_input();
ans = best_path(N,K,H,L);
if(ans==solution)
printf("Correct.\n");
else
printf("Incorrect. Returned %d, Expected %d.\n",ans,solution);
return 0;
}