#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());
roots.push_back(root);
used[root] = 1;
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
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:78:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(i=0; i<sons.size(); ++i)
| ~^~~~~~~~~~~~
fun.cpp:94:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for(i=0; i<sons.size(); ++i)
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Incorrect |
0 ms |
256 KB |
Tour is not fun |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Incorrect |
0 ms |
256 KB |
Tour is not fun |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Incorrect |
0 ms |
384 KB |
Tour is not fun |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Incorrect |
0 ms |
256 KB |
Tour is not fun |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Incorrect |
0 ms |
256 KB |
Tour is not fun |
12 |
Halted |
0 ms |
0 KB |
- |