답안 #1082128

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1082128 2024-08-30T17:51:42 Z Boas 가장 긴 여행 (IOI23_longesttrip) C++17
15 / 100
993 ms 4028 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
    {
        vsi adj(N);
        for (int i = 0; i < N; i++)
        {
            for (int j = i + 1; j < N; j++)
            {
                if (are_connected({i}, {j}))
                {
                    adj[i].insert(j);
                    adj[j].insert(i);
                }
            }
        }
        if (N <= 4)
        {
            vb vis(N);
            vi route;
            auto dfs = [&](auto &&self, int i) -> void
            {
                route.pb(i);
                vis[i] = 1;
                for (int j : adj[i])
                {
                    if (!vis[j])
                        self(self, j);
                }
            };
            int minAdj = 5;
            loop(N, i) minAdj = min(minAdj, sz(adj[i]));
            loop(N, i)
            {
                if (minAdj == sz(adj[i]))
                {
                    dfs(dfs, i);
                    break;
                }
            }
            assert(sz(route) == N);
            loop(N - 1, i) assert(adj[route[i]].count(route[i + 1]));
            return route;
        }
        si clique1, clique2;
        loop(N, i)
        {
            for (int j = i + 1; j < N; j++)
            {
                if (adj[i].count(j))
                    continue;
                if (clique1.empty())
                {
                    clique1.insert(i);
                    clique2.insert(j);
                }
                else
                {
                    bool iVal2 = 1, iVal1 = 1, jVal2 = 1, jVal1 = 1;
                    for (int k : clique1)
                    {
                        if (adj[i].count(k))
                        {
                            iVal2 = 0;
                        }
                        else
                        {
                            iVal1 = 0;
                        }
                        if (adj[j].count(k))
                        {
                            jVal2 = 0;
                        }
                        else
                        {
                            jVal1 = 0;
                        }
                    }
                    for (int k : clique2)
                    {
                        if (adj[i].count(k))
                        {
                            iVal1 = 0;
                        }
                        else
                        {
                            iVal2 = 0;
                        }
                        if (adj[j].count(k))
                        {
                            jVal1 = 0;
                        }
                        else
                        {
                            jVal2 = 0;
                        }
                    }
                    if (iVal1 || jVal2)
                    {
                        clique1.insert(i);
                        clique2.insert(j);
                    }
                    else if (iVal2 || jVal1)
                    {
                        clique2.insert(i);
                        clique1.insert(j);
                    }
                }
            }
        }
        loop(N, i)
        {
            if (clique1.count(i) || clique2.count(i))
                continue;
            bool iVal2 = 1, iVal1 = 1;
            for (int k : clique1)
            {
                if (!adj[i].count(k))
                {
                    iVal1 = 0;
                }
            }
            for (int k : clique2)
            {
                if (!adj[i].count(k))
                {
                    iVal2 = 0;
                }
            }
            if (iVal1)
            {
                clique1.insert(i);
            }
            else if (iVal2)
            {
                clique2.insert(i);
            }
            else
                throw;
        }
        ii bridge = {-1, -1};
        for (int i : clique1)
        {
            for (int j : adj[i])
            {
                if (clique2.count(j))
                {
                    bridge = {i, j};
                    break;
                }
            }
        }
        assert(sz(clique1) + sz(clique2) == N);
        for (int i : clique1)
        {
            for (int j : clique1)
            {
                // assert(i == j || adj[i].count(j));
            }
        }
        for (int i : clique2)
        {
            for (int j : clique2)
            {
                // assert(i == j || adj[i].count(j));
            }
        }
        vi res;
        if (bridge.F == -1)
        {
            if (sz(clique1) < sz(clique2))
            {
                swap(clique1, clique2);
            }
            for (int i : clique1)
                res.pb(i);
            // loop(sz(res) - 1, i) assert(adj[res[i]].count(res[i + 1]));
            return res;
        }
        for (int i : clique1)
        {
            if (i != bridge.F)
                res.pb(i);
        }
        res.pb(bridge.F);
        res.pb(bridge.S);
        for (int i : clique2)
        {
            if (i != bridge.S)
                res.pb(i);
        }
        assert(sz(res) == N);
        // loop(sz(res) - 1, i) assert(adj[res[i]].count(res[i + 1]));
        return res;
    }
    throw;
    return {};
}

Compilation message

longesttrip.cpp: In function 'vi longest_trip(int, int)':
longesttrip.cpp:243:22: warning: unused variable 'j' [-Wunused-variable]
  243 |             for (int j : clique1)
      |                      ^
longesttrip.cpp:241:18: warning: unused variable 'i' [-Wunused-variable]
  241 |         for (int i : clique1)
      |                  ^
longesttrip.cpp:250:22: warning: unused variable 'j' [-Wunused-variable]
  250 |             for (int j : clique2)
      |                      ^
longesttrip.cpp:248:18: warning: unused variable 'i' [-Wunused-variable]
  248 |         for (int i : clique2)
      |                  ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 344 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 5 ms 340 KB Output is correct
5 Correct 7 ms 348 KB Output is correct
6 Correct 5 ms 344 KB Output is correct
7 Correct 6 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 5 ms 344 KB Output is correct
11 Correct 8 ms 344 KB Output is correct
12 Correct 5 ms 344 KB Output is correct
13 Correct 7 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 28 ms 344 KB Output is correct
3 Correct 164 ms 344 KB Output is correct
4 Correct 415 ms 1524 KB Output is correct
5 Correct 912 ms 3840 KB Output is correct
6 Correct 9 ms 344 KB Output is correct
7 Correct 22 ms 344 KB Output is correct
8 Correct 186 ms 528 KB Output is correct
9 Correct 344 ms 752 KB Output is correct
10 Correct 949 ms 3432 KB Output is correct
11 Correct 919 ms 3348 KB Output is correct
12 Correct 956 ms 4028 KB Output is correct
13 Correct 993 ms 3808 KB Output is correct
14 Runtime error 1 ms 344 KB Execution killed with signal 6
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 24 ms 344 KB Output is correct
3 Partially correct 177 ms 344 KB Output is partially correct
4 Partially correct 457 ms 1292 KB Output is partially correct
5 Partially correct 899 ms 3528 KB Output is partially correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 24 ms 344 KB Output is correct
8 Partially correct 159 ms 856 KB Output is partially correct
9 Partially correct 345 ms 848 KB Output is partially correct
10 Partially correct 920 ms 3368 KB Output is partially correct
11 Partially correct 932 ms 3484 KB Output is partially correct
12 Partially correct 950 ms 3556 KB Output is partially correct
13 Partially correct 909 ms 3384 KB Output is partially correct
14 Runtime error 1 ms 600 KB Execution killed with signal 6
15 Halted 0 ms 0 KB -