| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 848650 | efedmrlr | Race (IOI11_race) | C++17 | 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>
#include "race.h"
using namespace std;
#define lli long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)
const double EPS = 0.00001;
const lli INF = 1e9+500;
const int ALPH = 26;
const int MOD = 1e9+7;
const int M = 1e6+5;
int n,m,q, k;
int cnt[M];
int ans = INF;
vector<int> vis, subt;
vector<vector<array<int,2> > > adj;
vector<int> centers;
void subtdfs(int node, int par) {
subt[node] = 1;
for(auto c : adj[node]) {
if(c[0] == par) continue;
if(vis[c[0]]) continue;
subtdfs(c[0], node);
subt[node] += subt[c[0]];
}
}
int cdfs(int node, int par, int sz) {
for(auto c : adj[node]) {
if(c[0] == par) continue;
if(vis[c[0]]) continue;
if(subt[c[0]] > sz/2) {
return cdfs(c[0], node, sz);
}
}
return node;
}
void find_centroids() {
subtdfs(1, 0);
int tmp = cdfs(1, 0, subt[1]);
centers.pb(tmp);
int ind = 0;
vis[tmp] = 1;
centers.pb(-1);
while(ind < centers.size()-1) {
if(centers[ind] == -1) {
centers.pb(-1);
ind++;
continue;
}
for(auto c : adj[centers[ind]]) {
if(vis[c[0]]) continue;
subtdfs(c[0], 0);
if(subt[c[0]] < 2) continue;
tmp = cdfs(c[0], 0, subt[c[0]]);
vis[tmp] = 1;
centers.pb(tmp);
}
ind++;
}
}
void dfs(int node ,int par, int cost, int leng) {
if(leng > k) return;
cnt[leng] = min(cnt[leng], cost);
for(auto c : adj[node]) {
if(c[0] == par) continue;
if(vis[c[0]]) continue;
dfs(c[0], node, cost + 1, leng + c[1]);
}
}
void dfs2(int node, int par, int cost, int leng) {
if(leng > k) return;
ans = min(ans ,cnt[k - leng] + cost);
for(auto c : adj[node]) {
if(c[0] == par) continue;
if(vis[c[0]]) continue;
dfs2(c[0], node, cost + 1, leng + c[1]);
}
}
void find_paths() {
for(int j = 0; j<k+1; j++) {
cnt[j] = INF;
}
cnt[0] = 0;
vis.clear();
vis.resize(n+1);
for(auto i : centers) {
if(i == -1) {
for(int j = 0; j<k+1; j++) {
cnt[j] = INF;
}
cnt[0] = 0;
continue;
}
vis[i] = 1;
for(auto c : adj[i]) {
dfs2(c[0], i, 1, c[1]);
dfs(c[0], i, 1, c[1]);
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N;
adj.resize(n+1); subt.resize(n+1); vis.resize(n+1,0);
k = K;
for(int i = 0; i<n-1; i++) {
H[i][0]++;
H[i][1]++;
adj[H[i][0]].pb({H[i][1], L[i]});
adj[H[i][1]].pb({H[i][0], L[i]});
}
find_centroids();
find_paths();
if(ans == INF) ans = -1;
return ans;
}
#define MAX_N 500000
static int N, K;
static int H[MAX_N][2];
static int L[MAX_N];
static int solution;
inline
void my_assert(int e) {if (!e) abort();}
void read_input()
{
int i;
my_assert(2==scanf("%d %d",&N,&K));
for(i=0; i<N-1; i++)
my_assert(3==scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]));
my_assert(1==scanf("%d",&solution));
}
int main()
{
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;
}
