답안 #965715

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
965715 2024-04-19T06:04:57 Z Darren0724 즐거운 행로 (APIO20_fun) C++17
0 / 100
3 ms 7260 KB
#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);
	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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7256 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Incorrect 3 ms 7256 KB Repeated index
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7256 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Incorrect 3 ms 7256 KB Repeated index
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7260 KB Output is correct
2 Incorrect 3 ms 7256 KB Repeated index
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7260 KB Output is correct
2 Incorrect 3 ms 7256 KB Repeated index
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7256 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Incorrect 3 ms 7256 KB Repeated index
4 Halted 0 ms 0 KB -