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;
const int Nmax = 1e5 + 5;
int n;
vector<int> ans;
bool used[Nmax];
int dist[Nmax];
void add(int node)
{
ans.push_back(node);
used[node] = 1;
}
void solve(int root)
{
int i, j;
for(i=0; i<n; ++i) dist[i] = hoursRequired(root, i);
vector<int> roots;
/// root = 0;
while(1)
{
vector<int> sons;
vector<vector<int>> d;
for(j=0; j<n; ++j)
if(!used[j] && dist[j] == dist[root] + 1)
sons.push_back(j);
if(sons.empty())
{
add(root);
break;
}
d.resize(sons.size());
for(j=0; j<n; ++j)
if(!used[j] && j != root)
{
int where = 0;
for(i=1; !where && i<sons.size(); ++i)
if(dist[j] == dist[sons[i]] + hoursRequired(sons[i], j))
where = i;
d[where].push_back(j);
}
for(i=0; i<sons.size(); ++i)
sort(d[i].begin(), d[i].end(), [&] (int x, int y) { return dist[x] < dist[y];} );
if(sons.size() == 1)
{
add(d[0].back());
add(root);
if(sons[0] == d[0].back()) break;
root = sons[0];
continue;
}
int last = -1;
while(1)
{
int best = -1, nd = -1;
for(i=0; i<sons.size(); ++i)
if(i != last && d[i].size() && dist[d[i].back()] > best)
best = dist[d[i].back()], nd = i;
if(nd == -1) break;
add(d[nd].back());
d[nd].pop_back();
last = nd;
}
// add(root);
roots.push_back(root);
used[root] = 1;
root = -1;
for(i=0; i<sons.size(); ++i)
if(d[i].size()) root = sons[i];
if(root == -1) break;
}
reverse(roots.begin(), roots.end());
for(auto it : roots) add(it);
}
vector<int> createFunTour(int N, int Q)
{
n = N;
solve(0);
return ans;
}
Compilation message (stderr)
fun.cpp: In function 'void solve(int)':
fun.cpp:50:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(i=1; !where && i<sons.size(); ++i)
| ~^~~~~~~~~~~~
fun.cpp:57:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(i=0; i<sons.size(); ++i)
| ~^~~~~~~~~~~~
fun.cpp:77:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(i=0; i<sons.size(); ++i)
| ~^~~~~~~~~~~~
fun.cpp:93:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for(i=0; i<sons.size(); ++i)
| ~^~~~~~~~~~~~
# | 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... |