Submission #26949

#TimeUsernameProblemLanguageResultExecution timeMemory
26949repeatingRace (IOI11_race)C++11
100 / 100
758 ms31436 KiB
#include <bits/stdc++.h> #include "race.h" #define F first #define S second #define P push #define pb push_back #define MEM(dp,i) memset(dp,i,sizeof(dp)) #define W while #define R return #define C continue #define SI size() #define ll long long #define ld long double #define pll pair<ll,ll> #define pii pair<int,int> #define SF(x) scanf("%I64d",&x) #define SF2(x,y) scanf("%I64d%I64d",&x,&y) #define SF3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z) #define SF4(x,y,z,o) scanf("%I64d%I64d%I64d%I64d",&x,&y,&z,&o) #define all(v) v.begin(),v.end() #define MAX_N 500000 using namespace std; const long long INF = 1e9+5e8; const int MX=200009; int n,k; int par[MX]; int sub[MX]; int dis[MX]; int ways[MX]; int t[MX*5]; vector<pii> adj[MX]; vector<int> v; bool blocked[MX]; stack<int> st; int DFS(int ver){ int ret=1; v.pb(ver); for(auto i : adj[ver]){ if(i.F==par[ver]||blocked[i.F])C; par[i.F]=ver; ret+=DFS(i.F); } sub[ver]=ret; R ret; } int dfs(int ver,ll d,int w,int p){ st.P(ver); if(d>1000000)R INF; int ret=INF; dis[ver]=d; ways[ver]=w; if(k>=d){ // cout<<t[k-d]<<endl; ret=min(ret,t[k-d]+w); } for(auto i : adj[ver]){ if(i.F==p||blocked[i.F])C; ret=min(ret,dfs(i.F,d+i.S,w+1,ver)); } R ret; } int solve(int ver){ int ret=INF; for(auto i : adj[ver]){ if(blocked[i.F])C; ret=min(ret,dfs(i.F,i.S,1,ver)); W(!st.empty()){ if(st.top()>=1000000){ st.pop(); C; } t[dis[st.top()]]=min(t[dis[st.top()]],ways[st.top()]); st.pop(); } } R ret; } int pick(int ver){ int mn=INF,pos=ver; for(auto i : v){ int mx=v.SI-sub[i]; for(auto j : adj[i]){ if(blocked[j.F]||j.F==par[i])C; mx=max(mx,sub[j.F]); } if(mx<mn){ mn=mx; pos=i; } } R pos; } int creat(int ver){ v.clear(); int ret=INF; DFS(ver); int pos=pick(ver); t[0]=0; ret=solve(pos); blocked[pos]=1; for(auto i : v){ par[i]=-1; if(0<=dis[i]&&dis[i]<=1000000) t[dis[i]]=INF; } for(auto i : adj[pos]){ if(blocked[i.F])C; ret=min(ret,creat(i.F)); } R ret; } int best_path(int N, int K, int H[][2], int L[]) { fill(t,t+1000001,INF); MEM(par,-1); 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]}); } int res=creat(0); if(res==INF)R -1; else R res; } //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; //}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...