이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 = 1e5 + 10;
int dp[N], h[N], d[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[x]) {
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(d[p] >= d[t]) {
t = p;
}
for(auto i : adj[p]) {
if(mark[i]) continue;
mark[i] = 1, d[i] = d[p] + 1, pq.push(i);
par[i] = p;
}
}
vector<pair<int, int> > vc;
vc.push_back({t, -1});
int k = par[t];
while(k != s) {
if(Sz(adj[k]) == 2) {
vc.push_back({k, -1});
k = par[k];
continue;
}
int u;
for(int i = 0; i < 3; i++) {
if(adj[k][i] != par[k] && adj[k][i] != vc.back().first) {
u = adj[k][i];
break;
}
}
vc.push_back({k, u});
k = par[k];
}
vc.push_back({s, -1});
memset(mark, 0, sizeof(mark));
vector<int> ans;
int r = Sz(vc) - 1, nu = 0;
bool f = 0, f2 = 0;
for(int l = 0; l < r; nu++) {
if(nu & 1) {
if(vc[l].second == -1 || f == 1) {
mark[vc[l].first] = 1;
ans.push_back(vc[l].first);
f = 0;
l++;
}
else {
f = 1;
mark[vc[l].second] = 1;
ans.push_back(vc[l].second);
}
}
else {
if(vc[r].second == -1 || f2 == 1) {
mark[vc[r].first] = 1;
ans.push_back(vc[r].first);
f2 = 0;
r--;
}
else {
f2 = 1;
mark[vc[r].second] = 1;
ans.push_back(vc[r].second);
}
}
}
if(vc[r].second != -1 && !mark[vc[r].second]) ans.push_back(vc[r].second);
ans.push_back(vc[r].first);
/*
for(auto i : ans) cout<<i <<" ";
cout<<endl;
*/
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... |