Submission #492542

#TimeUsernameProblemLanguageResultExecution timeMemory
4925421neRace (IOI11_race)C++14
0 / 100
11 ms15568 KiB
/* * author: Apiram * created: 08.12.2021 01:06:00 */ //#include "race.h" //#include <stdio.h> #include "race.h" #include<bits/stdc++.h> using namespace std; vector<vector<pair<int,int>>>adj(1e5); vector<vector<int>>sparce(1e5,vector<int>(20,-1)); vector<int>depth(1e5,0); vector<int64_t>dist(1e5,0); void getsparce(int u,int par){ for (auto x:adj[u]){ if (x.first!=par){ sparce[x.first][0] = u; depth[x.first]=depth[u]+1; dist[x.first] = dist[u] + x.second; getsparce(x.first,u); } } } int lca(int u,int v){ if (depth[u]<depth[v]){ swap(u,v); } for (int i = 19;i>=0;--i){ if (sparce[u][i]!=-1) if (depth[sparce[u][i]]>=depth[v]){ u=sparce[u][i]; } } if (u==v)return u; for (int i = 19;i>=0;--i){ if (sparce[u][i]!=sparce[v][i]){ u=sparce[u][i]; v=sparce[v][i]; } } return sparce[u][0]; } pair<int,int64_t> distt(int u,int v){ int l = lca(u,v); return {depth[u] + depth[v] - 2*depth[l],dist[u] + dist[v] - 2*dist[l]}; } void preprocess(int n){ getsparce(0,-1); for (int i = 1;i<20;++i){ for (int j = 0;j<n;++j){ if (sparce[j][i-1]!=-1){ sparce[j][i] = sparce[sparce[j][i-1]][i-1]; } } } } int anss = INT_MAX; void dfs(int left,int right,int k,int par1,int par2){ auto u = distt(left,right); if (u.second<k){ for (auto x:adj[right]){ if (x.first!=par2){ dfs(left,x.first,k,par1,right); } } } else if (u.second>k){ for (auto x:adj[left]){ if (x.first!=par1){ dfs(x.first,x.first,k,left,par2); } } } else{ anss = min(u.first,anss); } } int best_path(int n, int k, int H[][2], int L[]) { for (int i = 0;i<n-1;++i){ adj[H[i][0]].push_back({H[i][1],L[i]}); adj[H[i][1]].push_back({H[i][0],L[i]}); } preprocess(n); dfs(0,0,k,-1,-1); if (anss==INT_MAX){ anss = -1; } return anss; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...