Submission #858271

#TimeUsernameProblemLanguageResultExecution timeMemory
858271YassirSalamaRace (IOI11_race)C++17
0 / 100
3 ms14684 KiB
#include "race.h" #include <iostream> #include <vector> #include <algorithm> #include <unordered_map> #include <set> #include <unordered_set> #include <iomanip> #include <cmath> #include <limits> #include <map> #include <utility> #include <cctype> #include <string> #include <cstring> #include <stack> #include <queue> #include <functional> #include <iterator> using namespace std; #define OVL(x,s) for(auto y:x) cout<<y<<s; cout<<"\n"; void dbg_out() { cout << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); } #define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__); #define endl "\n" #define pb push_back #define F first #define S second #define ll long long #define mod 1000000007 #define all(v) v.begin(),v.end() const int MAXN=2e5+100; const int LOGN=17; int up[MAXN][LOGN+1]; int par[MAXN]; int n,k; set<pair<int,int>> v[MAXN]; int depth[MAXN];//the length of the unweightedf path from root to x int depth2[MAXN];//the length of the weighred path from root to x void dfslca(int node,int par,int dep){ depth[node]=depth[par]+1; depth2[node]=dep; up[node][0]=par; for(int i=1;i<=LOGN;i++){ up[node][i]=up[up[node][i-1]][i-1]; } for(auto x:v[node]){ if(x.F==par) continue; dfslca(x.F,node,dep+x.S); } } int LCA(int a,int b){ if(depth[a]<depth[b]) swap(a,b); int d=depth[a]-depth[b]; for(int i=LOGN;i>=0;i--){ if((1<<i)&d) a=up[a][i]; } if(a==b) return a; for(int i=LOGN;i>=0;i--){ if(up[a][i]==up[b][i]) continue; a=up[a][i]; b=up[b][i]; } return up[a][0]; } int sub[MAXN]; bool visited[MAXN]; int dfs(int node,int par){ if(visited[node]) return 0; sub[node]=1; for(auto x:v[node]){ if(x.F==par) continue; sub[node]+=dfs(x.F,node); } return sub[node]; } int dfs2(int node,int par,int n){ for(auto x:v[node]){ if(x.F==par) continue; if(!visited[x.F]&&sub[x.F]>n/2) return dfs2(x.F,node,n); } return node; } void decompose(int node,int parr){ dfs(node,-1); int c=dfs2(node,-1,sub[node]); visited[c]=true; par[c]=parr; for(auto x:v[c]){ if(!visited[x.F]) decompose(x.F,c); } } map<int,map<int,pair<int,int>>> mp; const int INF=1e9; int dist2(int a,int b){ return depth2[a]+depth2[b]-2*depth2[LCA(a,b)]; } int dist1(int a,int b){ return depth[a]+depth[b]-2*depth[LCA(a,b)]; } void update(int u){ int node=u; while(node!=-1){ int a=dist2(node,u); int b=dist1(node,u); if(mp[node].count(a)&& mp[node][a].F==b){ mp[node][a].S++; }else{ if(mp[node].count(a)) mp[node][a].F=min(mp[node][a].F,b); mp[node][a].S=1; } node=par[node]; } } int ANS=INF; void query(int u){ int node=u; while(node!=-1){ int a=dist2(node,u); int b=dist1(node,u); int target=k-a; // dbg(node,u,target,k,a); if(mp[node].count(target)){ if(a==target&&mp[node][a].S>=2) { ANS=min(ANS,b+mp[node][a].S); } else if(a!=target){ ANS=min(ANS,b+mp[node][target].S); } } node=par[node]; } } int best_path(int N, int K, int H[][2], int L[]) { for(int i=0;i<N-1;i++) { v[H[i][0]].insert({H[i][1],L[i]}); v[H[i][1]].insert({H[i][0],L[i]}); } k=K; dfslca(1,1,0); decompose(0,-1); // for(int i=0;i<N;i++) dbg(i,par[i]); for(int i=0;i<N;i++) update(i); for(int i=0;i<N;i++) query(i); if(ANS==1e9) return -1; return ANS; } #ifdef IOI #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; } #endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...