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], 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;
}
bool cmp(int i, int j) {
return d[i] > d[j];
}
bool cmp2(int i, int j) {
return d2[i] > d2[j];
}
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;
}
}
/* 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;*/
memset(mark, 0, sizeof(mark));
pq.push(t), mark[t] = 1;
while(!pq.empty()) {
int p = pq.front(); pq.pop();
for(auto i : adj[p]) {
if(mark[i]) continue;
mark[i] = 1, d2[i] = d2[p] + 1, pq.push(i);
}
}
vector<int> vc_s, vc_t;
for(int i = 0; i < N; i++) vc_s.push_back(i), vc_t.push_back(i);
sort(vc_s.begin(), vc_s.end(), cmp);
sort(vc_t.begin(), vc_t.end(), cmp2);
memset(mark, 0, sizeof(mark));
vector<int> ans;
int l = 0, r = 0, nu = 0;
while(nu < Sz(vc_s)) {
if(nu % 2 == 0) {
while(mark[vc_s[l]]) l++;
mark[vc_s[l]] = 1;
ans.push_back(vc_s[l]);
nu++;
}
else {
while(mark[vc_t[r]]) r++;
mark[vc_t[r]] = 1;
ans.push_back(vc_t[r]);
nu++;
}
}
return ans;
}
Compilation message (stderr)
fun.cpp: In function 'std::vector<int> createFunTour(int, int)':
fun.cpp:142:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
142 | while(nu < Sz(vc_s)) {
| ^
# | 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... |