# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
998191 |
2024-06-13T11:41:53 Z |
MarwenElarbi |
Race (IOI11_race) |
C++17 |
|
0 ms |
0 KB |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ii pair<int,int>
#define MAX_N 500000
const int nax=2e5+5;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
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));
}
vector<pair<int,int>> adj[nax];
int sz[nax];
bool removed[nax];
vector<vector<pair<ll,pair<ll,int>>>> dist(nax);
vector<map<int,int>> mp(nax);
int get_tree_size(int x,int p){
sz[x]=1;
for(auto u:adj[x]){
if(u.fi==p||removed[u.fi]) continue;
sz[x]+=get_tree_size(u.fi,x);
}
return sz[x];
}
int get_centroid(int x,int t_sz,int p){
for(auto u:adj[x]){
if(u.fi==p||removed[u.fi]) continue;
if(sz[u.fi]*2>t_sz){
return get_centroid(u.fi,t_sz,x);
}
}
return x;
}
void get_dist(int x,int centroid,int p,pair<ll,int> cur){
for(auto u:adj[x]){
if(u.fi==p||removed[u.fi]) continue;
cur.fi+=u.se;
cur.se++;
get_dist(u.fi,centroid,x,cur);
cur.fi-=u.se;
cur.se--;
}
//cout <<x<<" "<<centroid<<" "<<cur.fi<<" "<<cur.se<<endl;
if(mp[centroid].count(cur.fi)) mp[centroid][cur.fi]=min(mp[centroid][cur.fi],cur.se);
else mp[centroid][cur.fi]=cur.se;
dist[x].pb({centroid,cur});
}
void build(int x){
int centroid=get_centroid(x,get_tree_size(x,-1),-1);
for(auto u:adj[centroid]){
if(removed[u.fi]) continue;
get_dist(u.fi,centroid,centroid,make_pair(u.se,1));
}
removed[centroid]=true;
for(auto u:adj[centroid]){
if(removed[u.fi]) continue;
build(u.fi);
}
}
int n,k;
int query(int x){
int ans=1e9;
if(mp[x].count(k)){
ans=mp[x][k];
//cout <<x<<endl;
}
for(auto u:dist[x]){
if(!u.se.fi) continue;
if(mp[u.fi].count(k-u.se.fi)){
//cout <<x<<" "<<u.fi<<" "<<u.se.fi<<" "<<u.se.se<<" "<<k<<" "<<mp[u.fi][k-u.se.fi]<<endl;
ans=min(ans,u.se.se+mp[u.fi][k-u.se.fi]);
}
}
return ans;
}
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]].pb({H[i][1],L[i]});
adj[H[i][1]].pb({H[i][0],L[i]});
}
build(0);
int ans=1e9;
for (int i = 0; i < n; ++i)
{
int cur=query(i);
ans=min(ans,cur);
}
return ans;
}
int main()
{
/*#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;
}
Compilation message
/usr/bin/ld: /tmp/ccWq2LEG.o: in function `read_input()':
race.cpp:(.text+0x50): multiple definition of `read_input()'; /tmp/ccdWbvIG.o:grader.cpp:(.text+0x0): first defined here
/usr/bin/ld: /tmp/ccWq2LEG.o: in function `main':
race.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccdWbvIG.o:grader.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status