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>
#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});
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) {
ans.push_back(vc[l].first);
f = 0;
l++;
}
else {
f = 1;
ans.push_back(vc[l].second);
}
}
else {
if(vc[r].second == -1 || f2 == 1) {
ans.push_back(vc[r].first);
f2 = 0;
r--;
}
else {
f2 = 1;
ans.push_back(vc[r].second);
}
}
}
if(vc[r].second != -1 && f2 == 1) 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... |