Submission #965716

#TimeUsernameProblemLanguageResultExecution timeMemory
965716Darren0724Fun Tour (APIO20_fun)C++17
0 / 100
7 ms14684 KiB
#include "fun.h" #include <bits/stdc++.h> using namespace std; #define all(x) x.begin(),x.end() const int N=200005; vector<int> adj[N],sz(N,1),pa(N),ans,dis(N); vector<int> t[2]; void dfs(int k){ for(int j:adj[k]){ if(j==pa[k])continue; pa[j]=k; dfs(j); sz[k]+=sz[j]; } } int cen(int k,int need){ for(int j:adj[k]){ if(j==pa[k])continue; if(sz[j]>=need)return cen(j,need); } return k; } void dfs1(int id,int k,int pa){ t[id].push_back(k); for(int j:adj[k]){ if(j==pa)continue; dis[j]=dis[k]+1; dfs1(id,j,k); } } vector<int> createFunTour(int n, int q) { //int H = hoursRequired(0, N - 1); //int A = attractionsBehind(0, N - 1); for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(hoursRequired(i,j)==1){ adj[i].push_back(j); adj[j].push_back(i); } } } dfs(0); int c1=cen(0,(n+1)/2); int c2=pa[c1]; dfs1(0,c1,c2); dfs1(1,c2,c1); assert(c1!=c2); int sz=(int)t[0].size()-t[1].size(); assert(abs(sz)<=1); if(t[0].size()<t[1].size()){ swap(t[0],t[1]); } sort(all(t[0]),[&](int a,int b){return dis[a]<dis[b];}); sort(all(t[1]),[&](int a,int b){return dis[a]<dis[b];}); for(int i=0;i<n;i++){ ans.push_back(t[i&1][i>>1]); } reverse(all(ans)); return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...