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"
using namespace std;
#ifndef EVAL
#include "grader.cpp"
#endif
#define ar array
const int B = 894;
int d[B][B];
const int MAXN = 1e5 + 5;
vector<int> edges[MAXN];
vector<int> createFunTour(int n, int Q) {
if(n == 2) return {0, 1};
vector<int> d(n), sub(n);
for(int i=0;i<n;i++){
sub[i] = attractionsBehind(0, i);
}
int p = 0;
for(int i=1;i<n;i++){
if(sub[i] * 2 > n && sub[i] < sub[p]){
p = i;
}
}
vector<int> c;
for(int i=0;i<n;i++){
d[i] = hoursRequired(p, i);
if(d[i] == 1) c.push_back(i);
}
int sz = c.size();
assert(sz >= 2);
vector<priority_queue<ar<int, 2>>> v(sz);
for(int i=0;i<n;i++){
if(i == p) continue;
int d1 = hoursRequired(i, c[0]), d2 = hoursRequired(i, c[1]);
if(d1 == d2) v[2].push({d[i], i});
else {
if(d1 < d2) v[0].push({d[i], i});
else v[1].push({d[i], i});
}
}
vector<int> P;
int last = -1;
while((int)P.size() < n - 1){
vector<int> t(sz); iota(t.begin(), t.end(), 0);
sort(t.begin(), t.end(), [&](int i, int j){
if(v[i].empty()) return false;
if(v[j].empty()) return true;
return (v[i].top()[0] > v[j].top()[0]);
});
int a = t[0];
if(a == last && !v[t[1]].empty()) a = t[1];
P.push_back(v[a].top()[1]);
v[a].pop(); last = a;
sort(t.begin(), t.end(), [&](int i, int j){
return (v[i].size() > v[j].size());
});
if(sz > 2 && v[t[0]].size() > v[t[1]].size() + v[t[2]].size()){
assert(last == t[1] || last == t[2]);
int r = t[1] + t[2] - last;
if(!v[r].empty() && v[r].top()[0] > d[P.back()]){
P.push_back(v[r].top()[1]);
v[r].pop();
last = r;
}
while((int)P.size() < n - 1){
P.push_back(v[t[0]].top()[1]); v[t[0]].pop();
if(v[t[1]].empty() && v[t[2]].empty()) continue;
if(!v[t[1]].empty() && !v[t[2]].empty()){
if(v[t[1]].top()[0] > v[t[2]].top()[0]) P.push_back(v[t[1]].top()[1]), v[t[1]].pop();
else P.push_back(v[t[2]].top()[1]), v[t[2]].pop();
} else if(v[t[1]].empty()) P.push_back(v[t[2]].top()[1]), v[t[2]].pop();
else P.push_back(v[t[1]].top()[1]), v[t[1]].pop();
}
}
}
P.push_back(p);
return P;
}
/*
12 400000
0 1
0 2
1 3
1 4
2 5
2 6
3 7
3 8
4 9
4 10
5 11
*/
# | 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... |