Submission #1082452

# Submission time Handle Problem Language Result Execution time Memory
1082452 2024-08-31T11:15:50 Z Boas Longest Trip (IOI23_longesttrip) C++17
15 / 100
897 ms 1400 KB
#include "longesttrip.h"

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

template <typename T1, typename T2>
using indexed_map = tree<T1, T2, less<T1>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using indexed_set = indexed_map<T, null_type>;

#define loop(x, i) for (int i = 0; i < (x); i++)
#define loop1(x, i) for (int i = 1; i <= (x); i++)
#define rev(x, i) for (int i = (int)(x) - 1; i >= 0; i--)
#define itloop(x) for (auto it = begin(x); x != end(x); it++)
#define itrev(x) for (auto it = rbegin(x); x != rend(x); it++)
#define INF32 ((int32_t)(2e9 + 1))
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
#define removeIn(x, l) l.erase(find(ALL(l), x))
#define pb push_back
#define sz(x) (int)(x).size()
#define F first
#define S second
#define var const auto &
#define foreach(l) for (var e : l)

typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<i32> vi32;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vi32> vvi32;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vii> vvii;
typedef vector<viii> vviii;
typedef set<int> si;
typedef set<ii> sii;
typedef set<iii> siii;
typedef vector<si> vsi;
typedef vector<sii> vsii;
typedef vector<vsi> vvsi;
typedef vector<string> vstr;
typedef vector<vector<string>> vvstr;
typedef vector<bool> vb;
typedef vector<vb> vvb;

vi longest_trip(int N, int D)
{
    if (D >= 2)
    {
        vi res(N);
        iota(ALL(res), 0);
        if (D == 3)
        {
            return res;
        }
        si resterend;
        loop1(N - 1, i) resterend.insert(res[i]);
        loop1(N - 1, i)
        {
            auto it = resterend.begin();
            if (are_connected({res[i - 1]}, {*it}))
            {
                res[i] = *it;
                resterend.erase(it);
            }
            else
            {
                if (resterend.size() == 1)
                {
                    res.insert(res.begin(), *it);
                    res.pop_back();
                    return res;
                }
                res[i] = *next(it);
                resterend.erase(next(it));
            }
        }
        return res;
    }
    else
    {
        vvi adj(N);
        bitset<256> con[256];
        for (int i = 0; i < N; i++)
        {
            for (int j = i + 1; j < N; j++)
            {
                if (are_connected({i}, {j}))
                {
                    // cerr << i << ' ' << j << endl;
                    adj[i].pb(j);
                    adj[j].pb(i);
                    con[i][j] = 1;
                    con[j][i] = 1;
                }
            }
        }
        vb vis(N);
        int cnt = 0;
        auto dfs = [&](auto &&self, int i) -> void
        {
            vis[i] = 1;
            cnt++;
            for (int j : adj[i])
            {
                if (!vis[j])
                    self(self, j);
            }
        };
        dfs(dfs, 0);
        vi res;
        if (cnt != N)
        {
            int mx = max(cnt, N - cnt);
            loop(N, i)
            {
                if (vis[i] == cnt == mx)
                    res.pb(i);
            }
            loop(sz(res) - 1, i) assert(con[res[i]][res[i + 1]]);
            return res;
        }
        deque<int> path;
        path.pb(0);
        int i2 = *adj[0].begin();
        path.pb(i2);
        int i3 = -1;
        for (int j : adj[i2])
        {
            if (j != 0)
            {
                i3 = j;
                path.pb(i3);
                break;
            }
        }
        if (i3 == -1)
        {
            for (int j : adj[0])
            {
                if (j != i2)
                {
                    i3 = j;
                    path.push_front(i3);
                    break;
                }
            }
        }
        deque<int> left;
        for (int i = 1; i < N; i++)
            if (i != i2 && i != i3)
                left.pb(i);
        while (!left.empty())
        {
            int i = left.back();
            left.pop_back();
            if (con[i][path[0]])
            {
                path.push_front(i);
            }
            else if (con[i][path.back()])
            {
                path.push_back(i);
            }
            else
            {
                assert(con[path[0]][path.back()]);
                int newBegin = -1;
                for (int j = 1; j < sz(path) - 1; j++)
                {
                    if (con[i][path[j]])
                    {
                        newBegin = j;
                        break;
                    }
                }
                if (newBegin == -1)
                {
                    left.push_front(i);
                    continue;
                }
                deque<int> newPath;
                newPath.pb(i);
                for (int j = newBegin; j < sz(path); j++)
                {
                    newPath.pb(path[j]);
                }
                for (int j = 0; j < newBegin; j++)
                {
                    newPath.pb(path[j]);
                }
                path = newPath;
            }
        }
        for (int v : path)
            res.pb(v);
        assert(sz(res) == N);
        loop(sz(res) - 1, i) assert(con[res[i]][res[i + 1]]);
        return res;
    }
    throw;
    return {};
}

Compilation message

longesttrip.cpp: In function 'vi longest_trip(int, int)':
longesttrip.cpp:127:28: warning: suggest parentheses around comparison in operand of '==' [-Wparentheses]
  127 |                 if (vis[i] == cnt == mx)
      |                     ~~~~~~~^~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB Incorrect
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 412 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 6 ms 344 KB Output is correct
4 Correct 7 ms 344 KB Output is correct
5 Correct 8 ms 344 KB Output is correct
6 Correct 5 ms 344 KB Output is correct
7 Correct 5 ms 344 KB Output is correct
8 Correct 5 ms 344 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 7 ms 344 KB Output is correct
11 Correct 6 ms 344 KB Output is correct
12 Correct 6 ms 344 KB Output is correct
13 Correct 5 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 344 KB Output is correct
2 Correct 27 ms 344 KB Output is correct
3 Correct 175 ms 344 KB Output is correct
4 Correct 430 ms 708 KB Output is correct
5 Correct 860 ms 1144 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 32 ms 344 KB Output is correct
8 Correct 160 ms 344 KB Output is correct
9 Correct 324 ms 604 KB Output is correct
10 Correct 855 ms 980 KB Output is correct
11 Correct 804 ms 864 KB Output is correct
12 Correct 840 ms 1072 KB Output is correct
13 Correct 847 ms 1228 KB Output is correct
14 Incorrect 1 ms 344 KB Incorrect
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 26 ms 344 KB Output is correct
3 Partially correct 136 ms 344 KB Output is partially correct
4 Partially correct 406 ms 748 KB Output is partially correct
5 Partially correct 855 ms 992 KB Output is partially correct
6 Correct 8 ms 340 KB Output is correct
7 Correct 28 ms 344 KB Output is correct
8 Partially correct 173 ms 600 KB Output is partially correct
9 Partially correct 317 ms 500 KB Output is partially correct
10 Partially correct 853 ms 1400 KB Output is partially correct
11 Partially correct 855 ms 1004 KB Output is partially correct
12 Partially correct 897 ms 784 KB Output is partially correct
13 Partially correct 897 ms 984 KB Output is partially correct
14 Incorrect 0 ms 344 KB Incorrect
15 Halted 0 ms 0 KB -