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);
/// 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);
return;
}
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()) return;
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);
root = -1;
for(i=0; i<sons.size(); ++i)
if(d[i].size()) root = sons[i];
if(root == -1) return;
}
}
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:48:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for(i=1; !where && i<sons.size(); ++i)
| ~^~~~~~~~~~~~
fun.cpp:55:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(i=0; i<sons.size(); ++i)
| ~^~~~~~~~~~~~
fun.cpp:75:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(i=0; i<sons.size(); ++i)
| ~^~~~~~~~~~~~
fun.cpp:89:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | 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... |