Submission #576638

#TimeUsernameProblemLanguageResultExecution timeMemory
576638Arnch즐거운 행로 (APIO20_fun)C++17
26 / 100
105 ms14764 KiB
#include "fun.h"
#include<bits/stdc++.h>

#define H(x, y) hoursRequired(x, y)
#define A(x, y) attractionsBehind(x, y)
#define Sz(x) x.size()

using namespace std;

const int N = 1e3 + 10;

int dp[N], h[N], d[N][N], d2[N], par[N];
bool mark[N];
vector<int> adj[N];

int dfs(int x, int p = 0) {
	int res = x;
	for(auto i : adj[x]) {
		if(i == p) continue;
		h[i] = h[x] + 1;
		int u = dfs(i, x);
		if(h[u] > h[res]) {
			res = u;
		}
	}
	return res;
}

vector<int> createFunTour(int N, int Q) {
	for(int i = 1; i < N; i++) {
		for(int j = 0; j < i; j++) {
			if(H(i, j) == 1) {
				adj[i].push_back(j), adj[j].push_back(i);
			}
		}
	}
	
	int s = dfs(0), t = 0;
	queue<int> pq;
	pq.push(s), mark[s] = 1, par[s] = s;

	while(!pq.empty()) {
		int p = pq.front(); pq.pop();
		if(d2[p] >= d2[t]) {
			t = p;
		}
		for(auto i : adj[p]) {
			if(mark[i]) continue;
			mark[i] = 1, d2[i] = d2[p] + 1, pq.push(i);
			par[i] = p;
		}
	}

/*	memset(mark, 0, sizeof(mark));
	vector<vector<int> > vc;
	vc.push_back({t}), mark[t] = 1;
	int k = par[t];
	while(k != s) {
		if(Sz(adj[k]) == 2) {
			vc.push_back({k}), mark[k] = 1;
			k = par[k];
			continue;
		}
		vector<int> st;
		queue<int> pq; 
		pq.push(k), mark[k] = 1, d[k] = 0;
		while(!pq.empty()) {
			int p = pq.front(); pq.pop();
			st.push_back(p);
			for(auto i : adj[p]) {
				if(mark[i] == 1 || i == par[k]) continue;
				d[i] = d[p] + 1, mark[i] = 1, pq.push(i);
			}
		}
		vc.push_back(st);
		k = par[k];
	}
	vc.push_back({s});

	memset(mark, 0, sizeof(mark));
	
	vector<int> ans;
	int r = Sz(vc) - 1, nu = 0;
	for(int l = 0; l < r; nu++) {
		if(nu & 1) {
			mark[vc[l].back()] = 1;
			ans.push_back(vc[l].back());
			vc[l].pop_back();
			if(vc[l].empty()) l++;
		}
		else {
			mark[vc[r].back()] = 1;
			ans.push_back(vc[r].back());
			vc[r].pop_back();
			if(vc[r].empty())
				r--;
		}
	}
	
	int ptr = 0;
	while(!vc[r].empty() && ptr < Sz(vc[r])) {
		ans.push_back(vc[r].back());
		ans.pop_back();
		if(ptr < Sz(vc[r])) {
			ans.push_back(vc[r][ptr]);
			ptr++;
		}
	}

	for(auto i : ans) cout<<i <<" ";
	cout<<endl;*/


	for(int i = 0; i < N; i++) {
		memset(mark, 0, sizeof(mark));
		pq.push(i), mark[i] = 1;

		while(!pq.empty()) {
			int p = pq.front(); pq.pop();
			for(auto j : adj[p]) {
				if(mark[j]) continue;
				mark[j] = 1, d[i][j] = d[i][p] + 1, pq.push(j);
			}
		}
	}
	
	memset(mark, 0, sizeof(mark));
	mark[s] = 1;
	int nu = 1;
	vector<int> ans;
	ans.push_back(s);
	while(nu < N) {
		int v = ans.back();
		int mx = -1;
		for(int i = 0; i < N; i++) {
			if(mark[i]) continue;
			if(mx == -1 || d[v][i] >= d[v][mx]) {
				mx = i;
			}
		}
		ans.push_back(mx);
		mark[mx] = 1;
		nu++;
	}
		

	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...