Submission #877462

#TimeUsernameProblemLanguageResultExecution timeMemory
877462boris_mihovFun Tour (APIO20_fun)C++17
Compilation error
0 ms0 KiB
#include "fun.h"
#include <algorithm>
#include <iostream>
#include <cassert>
#include <numeric>
#include <vector>
#include <queue>

typedef long long llong;
const int MAXN = 100000 + 10;
const int INF  = 1e9;

int n;
int sz[MAXN];
int pos[MAXN];
int type[MAXN];
int perm[MAXN];
int depth[MAXN];
std::vector <int> g[MAXN];
int waitlistType = -1;

struct Element
{
    int type;
    int depth;
    int node;

    friend bool operator < (const Element &a, const Element & b)
    {
        return a.depth < b.depth || (a.depth == b.depth && a.node > b.node);
    }
};

std::priority_queue <Element> pq;
std::queue <Element> q;

void calcSize(int node, int par)
{
    sz[node] = 1;
    for (const int &u : g[node])
    {
        if (u == par)
        {
            continue;
        }

        calcSize(u, node);
        sz[node] += sz[u];
    }
}

int findCentroid(int node, int par)
{
    for (const int &u : g[node])
    {
        if (u != par && sz[u] > n / 2)
        {
            return findCentroid(u, node);
        }
    }

    return node;
}

void assign(int node, int par, int t)
{
    type[node] = t;
    depth[node] = depth[par] + 1;
    for (const int &u : g[node])
    {
        if (u == par)
        {
            continue;
        }

        assign(u, node, t);
    }
}

std::vector <int> createFunTour(int N, int Q) 
{  
    n = N;
    for (int i = 1 ; i <= n ; ++i)
    {
        for (int j = 2 ; j <= n ; ++j)
        {
            if (hoursRequired(i - 1, j - 1) == 1)
            {
                g[i].push_back(j);
                g[j].push_back(i);
            }
        }
    }

    calcSize(1, 0);
    int cntr = findCentroid(1, 0);

    for (int idx = 0 ; idx < g[cntr].size() ; ++idx)
    {
        assign(g[cntr][idx], cntr, idx);
    }

    type[cntr] = g[cntr].size();
    std::vector <int> answer;

    for (int i = 1 ; i <= n ; ++i)
    {
        pq.push({type[i], depth[i], i});
    }
    
    while (!pq.empty())
    {
        auto [t, d, node] = pq.top();
        pq.pop();  

        if (waitlistType == t)
        {
            q.push({t, d, node});
            continue;
        }

        if (waitlistType != -1)
        {
            answer.push_back(node);
            answer.push_back(q.front().node);
            q.pop();

            if (q.empty())
            {
                waitlistType = -1;
            }

            continue;
        }

        if (answer.size() && type[answer.back()] == t)
        {
            q.push({t, d, node});
            waitlistType = t;
            continue;
        }

        answer.push_back(node);
    }

    std::vector <int> output;
    for (int i = 0 ; i < answer.size() ; ++i)
    {
        output.push_back(answer[i] - 1);
        if (q.size() && (i + 2 > answer.size() || depth[answer[i + 2]] <= q.front().depth) && (type[answer[i]] != q.front().type && type[answer[i + 1]] != q.front().type))
        {
            output.push_back(q.front().node - 1);
            q.pop();
        }
    }

    while (!q.empty())
    {
        output.push_back(q.front());
        q.pop();
    }
    
    return output;
}

Compilation message (stderr)

fun.cpp: In function 'std::vector<int> createFunTour(int, int)':
fun.cpp:98:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |     for (int idx = 0 ; idx < g[cntr].size() ; ++idx)
      |                        ~~~~^~~~~~~~~~~~~~~~
fun.cpp:147:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  147 |     for (int i = 0 ; i < answer.size() ; ++i)
      |                      ~~^~~~~~~~~~~~~~~
fun.cpp:150:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  150 |         if (q.size() && (i + 2 > answer.size() || depth[answer[i + 2]] <= q.front().depth) && (type[answer[i]] != q.front().type && type[answer[i + 1]] != q.front().type))
      |                          ~~~~~~^~~~~~~~~~~~~~~
fun.cpp:159:35: error: no matching function for call to 'std::vector<int>::push_back(__gnu_cxx::__alloc_traits<std::allocator<Element>, Element>::value_type&)'
  159 |         output.push_back(q.front());
      |                                   ^
In file included from /usr/include/c++/10/vector:67,
                 from fun.h:1,
                 from fun.cpp:1:
/usr/include/c++/10/bits/stl_vector.h:1187:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::value_type = int]'
 1187 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/10/bits/stl_vector.h:1187:35: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<Element>, Element>::value_type' {aka 'Element'} to 'const value_type&' {aka 'const int&'}
 1187 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/10/bits/stl_vector.h:1203:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::value_type = int]'
 1203 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/10/bits/stl_vector.h:1203:30: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<Element>, Element>::value_type' {aka 'Element'} to 'std::vector<int>::value_type&&' {aka 'int&&'}
 1203 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~