This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |