Submission #839928

# Submission time Handle Problem Language Result Execution time Memory
839928 2023-08-30T21:17:45 Z That_Salamander Longest Trip (IOI23_longesttrip) C++17
15 / 100
1000 ms 2700 KB
#include <vector>

std::vector<int> longest_trip(int N, int D);

bool are_connected(std::vector<int> A, std::vector<int> B);

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;

int n;
bool adj[256][256];
vector<int> conn[256];

void makePath(vector<int>& nodes, vector<int>& out);

vector<int> secondary;
void makePaths(vector<int>& nodes, vector<int>& primary) {
    secondary.clear();
    if (nodes.size() <= 1) {
        for (int x: nodes) {
            primary.push_back(x);
        }
        return;
    }
    vi g1;
    vector<bool> mask(n, true);
    for (int x: nodes) {
        mask[x] = false;
    }

    queue<int> q;
    q.push(nodes[0]);

    while (!q.empty()) {
        auto t = q.front(); q.pop();
        if (mask[t]) continue;
        g1.push_back(t);
        mask[t] = true;

        for (int c: conn[t]) {
            q.push(c);
        }
    }

    if (g1.size() == nodes.size()) {
        makePath(nodes, primary);
        secondary.clear();
    } else {
        for (int x: g1) {
            primary.push_back(x);
        }
        for (int x: nodes) {
            if (!mask[x]) {
                secondary.push_back(x);
            }
        }
    }
}

#define MIDDLE 0
#define INTER 1
#define UPPER 2
#define LOWER 3

void makePath(vector<int>& nodes, vector<int>& res) {
    if (nodes.size() <= 2) {
        for (int x: nodes) {
            res.push_back(x);
        }
        return;
    }

    vector<int> nodeTypes(n, -1);

    int middleNode = nodes[0];
    nodeTypes[middleNode] = MIDDLE;

    vi lowerNodes, interNodes, upperNodes;

    for (int x: nodes) {
        if (nodeTypes[x] == -1 && !adj[x][middleNode]) {
            lowerNodes.push_back(x);
            nodeTypes[x] = LOWER;
        }
    }

    for (int x: nodes) {
        if (nodeTypes[x] != -1) continue;
        bool bruh = false;
        for (int y: lowerNodes) {
            if (adj[x][y]) {
                bruh = true;
                break;
            }
        }

        if (bruh) {
            interNodes.push_back(x);
            nodeTypes[x] = INTER;
        } else {
            upperNodes.push_back(x);
            nodeTypes[x] = UPPER;
        }
    }

    if (lowerNodes.size() == 0) {
        makePaths(upperNodes, res);

        res.push_back(middleNode);

        for (int x: secondary) {
            res.push_back(x);
        }
    } else if (lowerNodes.size() == 1) {
        for (int x: upperNodes) {
            res.push_back(x);
        }

        res.push_back(middleNode);

        makePaths(interNodes, res);

        res.push_back(lowerNodes[0]);

        for (int x: secondary) {
            res.push_back(x);
        }
    } else {
        for (int x: upperNodes) {
            res.push_back(x);
        }

        res.push_back(middleNode);

        makePaths(interNodes, res);

        int n1 = res.back();
        if (secondary.empty()) {
            int start;
            for (int x:lowerNodes) {
                if (adj[x][n1]) {
                    start = x;
                    break;
                }
            }

            res.push_back(start);

            for (int x:lowerNodes) {
                if (x != start) res.push_back(x);
            }
        } else {
            int n2 = secondary.front();

            int a, b;

            vector<int> a1, a2;

            for (int x: lowerNodes) {
                if (adj[n1][x]) {
                    a1.push_back(x);
                }

                if (adj[n2][x]) {
                    a2.push_back(x);
                }
            }

            if (a1.size() > 1 && a2.size() > 1) {
                a = a1[0];
                b = a2[0] == a ? a2[1] : a2[0];
            } else if (a2.size() > 1) {
                a = a1[0];
                b = a2[0] == a ? a2[1] : a2[0];
            } else if (a1.size() > 1) {
                b = a2[0];
                a = a1[0] == b ? a1[1] : a1[0];
            } else {
                a = a1[0];
                b = a2[0];
            }

            out:

            res.push_back(a);

            for (int x: lowerNodes) {
                if (x != a && x != b) {
                    res.push_back(x);
                }
            }

            res.push_back(b);

            for (int x: secondary) {
                res.push_back(x);
            }
        }
    }
}

/*
1
5 1
0
1 1
0 1 0
1 0 0 1
*/

vector<int> longest_trip(int N, int D) {
    n = N;
    memset(adj, false, sizeof(adj));
    for (int i = 0; i < N; i++) {
        conn[i].clear();
    }

    vi allNodes;

    for (int i = 0; i < N; i++) {
        allNodes.push_back(i);
        for (int j = i+1; j < N; j++) {
            if (are_connected({i}, {j})) {
                adj[i][j] = adj[j][i] = true;

                conn[i].push_back(j);
                conn[j].push_back(i);
            }
        }
    }

    vector<int> primary;
    makePaths(allNodes, primary);

    if (primary.size() > secondary.size()) {
        return primary;
    } else {
        return secondary;
    }
}

#ifdef BRUH
static inline constexpr int maxNumberOfCalls = 32640;
static inline constexpr int maxTotalNumberOfCalls = 150000;
static inline constexpr int maxTotalNumberOfLandmarksInCalls = 1500000;
static int call_counter = 0;
static int total_call_counter = 0;
static int landmark_counter = 0;

static int C, N, D;
static std::vector<std::vector<int>> U;
static std::vector<bool> present;

static inline void protocol_violation(std::string message)
{
    printf("Protocol Violation: %s\n", message.c_str());
    exit(0);
}

bool are_connected(std::vector<int> A, std::vector<int> B)
{
    ++call_counter;
    ++total_call_counter;
    if (call_counter > maxNumberOfCalls || total_call_counter > maxTotalNumberOfCalls)
    {
        protocol_violation("too many calls");
    }

    int nA = A.size(), nB = B.size();
    landmark_counter += nA + nB;
    if (landmark_counter > maxTotalNumberOfLandmarksInCalls)
    {
        protocol_violation("too many elements");
    }

    if (nA == 0 || nB == 0)
    {
        protocol_violation("invalid array");
    }
    for (int i = 0; i < nA; ++i)
    {
        if (A[i] < 0 || N <= A[i])
        {
            protocol_violation("invalid array");
        }
        if (present[A[i]])
        {
            protocol_violation("invalid array");
        }
        present[A[i]] = true;
    }
    for (int i = 0; i < nA; ++i)
    {
        present[A[i]] = false;
    }
    for (int i = 0; i < nB; ++i)
    {
        if (B[i] < 0 || N <= B[i])
        {
            protocol_violation("invalid array");
        }
        if (present[B[i]])
        {
            protocol_violation("invalid array");
        }
        present[B[i]] = true;
    }
    for (int i = 0; i < nB; ++i)
    {
        present[B[i]] = false;
    }

    for (int i = 0; i < nA; ++i)
    {
        for (int j = 0; j < nB; ++j)
        {
            if (A[i] == B[j])
            {
                protocol_violation("non-disjoint arrays");
            }
        }
    }

    for (int i = 0; i < nA; ++i)
    {
        for (int j = 0; j < nB; ++j)
        {
            if (U[std::max(A[i], B[j])][std::min(A[i], B[j])] == 1)
            {
                return true;
            }
        }
    }

    return false;
}

int main()
{
    assert(1 == scanf("%d", &C));
    int maximumCalls = 0;
    for (int c = 0; c < C; ++c)
    {
        call_counter = 0;
        assert(2 == scanf("%d %d", &N, &D));

        present.assign(N, false);
        U.resize(N);
        for (int i = 1; i < N; ++i)
        {
            U[i].resize(i);
            for (int j = 0; j < i; ++j)
            {
                assert(1 == scanf("%d", &U[i][j]));
            }
        }

        for (int i = 2; i < N; ++i)
        {
            for (int j = 1; j < i; ++j)
            {
                for (int k = 0; k < j; ++k)
                {
                    if (U[i][j] + U[i][k] + U[j][k] < D)
                    {
                        printf("Insufficient Density\n");
                        exit(0);
                    }
                }
            }
        }

        std::vector<int> t = longest_trip(N, D);
        int l = t.size();
        printf("%d\n", l);
        for (int i = 0; i < l; ++i)
        {
            printf(i == 0 ? "%d" : " %d", t[i]);
        }
        printf("\n");
        printf("%d\n", call_counter);

        maximumCalls = std::max(maximumCalls, call_counter);
        call_counter = 0;
    }
    printf("%d\n", maximumCalls);

    return 0;
}
#endif

Compilation message

longesttrip.cpp: In function 'void makePath(std::vector<int>&, std::vector<int>&)':
longesttrip.cpp:186:13: warning: label 'out' defined but not used [-Wunused-label]
  186 |             out:
      |             ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 241 ms 792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 336 KB Output is correct
2 Correct 44 ms 336 KB Output is correct
3 Correct 221 ms 456 KB Output is correct
4 Correct 491 ms 860 KB Output is correct
5 Correct 971 ms 2672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 336 KB Output is correct
2 Correct 43 ms 336 KB Output is correct
3 Correct 205 ms 464 KB Output is correct
4 Correct 515 ms 880 KB Output is correct
5 Correct 920 ms 2596 KB Output is correct
6 Correct 10 ms 336 KB Output is correct
7 Correct 28 ms 336 KB Output is correct
8 Correct 135 ms 336 KB Output is correct
9 Correct 299 ms 564 KB Output is correct
10 Correct 849 ms 1840 KB Output is correct
11 Correct 839 ms 1656 KB Output is correct
12 Correct 800 ms 1692 KB Output is correct
13 Correct 844 ms 1664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 336 KB Output is correct
2 Correct 31 ms 336 KB Output is correct
3 Correct 164 ms 460 KB Output is correct
4 Correct 451 ms 988 KB Output is correct
5 Correct 896 ms 2700 KB Output is correct
6 Correct 11 ms 336 KB Output is correct
7 Correct 29 ms 336 KB Output is correct
8 Correct 145 ms 516 KB Output is correct
9 Correct 325 ms 696 KB Output is correct
10 Correct 821 ms 1872 KB Output is correct
11 Correct 877 ms 1712 KB Output is correct
12 Correct 848 ms 1728 KB Output is correct
13 Correct 834 ms 1548 KB Output is correct
14 Correct 10 ms 336 KB Output is correct
15 Correct 17 ms 336 KB Output is correct
16 Correct 47 ms 336 KB Output is correct
17 Correct 96 ms 336 KB Output is correct
18 Correct 151 ms 392 KB Output is correct
19 Correct 269 ms 520 KB Output is correct
20 Correct 296 ms 464 KB Output is correct
21 Correct 948 ms 896 KB Output is correct
22 Correct 966 ms 1000 KB Output is correct
23 Correct 981 ms 888 KB Output is correct
24 Correct 874 ms 908 KB Output is correct
25 Correct 12 ms 336 KB Output is correct
26 Correct 15 ms 336 KB Output is correct
27 Correct 38 ms 336 KB Output is correct
28 Correct 33 ms 336 KB Output is correct
29 Correct 37 ms 336 KB Output is correct
30 Correct 211 ms 396 KB Output is correct
31 Correct 194 ms 388 KB Output is correct
32 Correct 160 ms 336 KB Output is correct
33 Correct 270 ms 336 KB Output is correct
34 Correct 287 ms 600 KB Output is correct
35 Correct 325 ms 508 KB Output is correct
36 Correct 753 ms 888 KB Output is correct
37 Correct 812 ms 804 KB Output is correct
38 Correct 865 ms 816 KB Output is correct
39 Correct 855 ms 764 KB Output is correct
40 Correct 853 ms 708 KB Output is correct
41 Correct 776 ms 580 KB Output is correct
42 Correct 842 ms 500 KB Output is correct
43 Correct 845 ms 652 KB Output is correct
44 Correct 831 ms 660 KB Output is correct
45 Correct 12 ms 336 KB Output is correct
46 Correct 12 ms 336 KB Output is correct
47 Correct 30 ms 336 KB Output is correct
48 Correct 29 ms 336 KB Output is correct
49 Correct 29 ms 336 KB Output is correct
50 Correct 204 ms 396 KB Output is correct
51 Correct 214 ms 392 KB Output is correct
52 Correct 199 ms 336 KB Output is correct
53 Correct 270 ms 392 KB Output is correct
54 Correct 320 ms 452 KB Output is correct
55 Correct 249 ms 400 KB Output is correct
56 Correct 765 ms 976 KB Output is correct
57 Correct 740 ms 900 KB Output is correct
58 Correct 800 ms 848 KB Output is correct
59 Correct 959 ms 760 KB Output is correct
60 Execution timed out 1054 ms 844 KB Time limit exceeded
61 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 336 KB Output is correct
2 Correct 42 ms 336 KB Output is correct
3 Partially correct 223 ms 392 KB Output is partially correct
4 Partially correct 515 ms 1056 KB Output is partially correct
5 Partially correct 992 ms 2672 KB Output is partially correct
6 Correct 11 ms 336 KB Output is correct
7 Correct 35 ms 368 KB Output is correct
8 Partially correct 173 ms 464 KB Output is partially correct
9 Partially correct 321 ms 568 KB Output is partially correct
10 Partially correct 930 ms 1796 KB Output is partially correct
11 Partially correct 828 ms 1720 KB Output is partially correct
12 Partially correct 1000 ms 1560 KB Output is partially correct
13 Partially correct 828 ms 1668 KB Output is partially correct
14 Correct 13 ms 336 KB Output is correct
15 Correct 11 ms 336 KB Output is correct
16 Correct 73 ms 336 KB Output is correct
17 Partially correct 133 ms 336 KB Output is partially correct
18 Partially correct 205 ms 388 KB Output is partially correct
19 Partially correct 410 ms 492 KB Output is partially correct
20 Partially correct 386 ms 516 KB Output is partially correct
21 Correct 11 ms 336 KB Output is correct
22 Correct 16 ms 336 KB Output is correct
23 Correct 38 ms 336 KB Output is correct
24 Correct 28 ms 336 KB Output is correct
25 Correct 29 ms 336 KB Output is correct
26 Partially correct 251 ms 376 KB Output is partially correct
27 Partially correct 213 ms 336 KB Output is partially correct
28 Partially correct 258 ms 336 KB Output is partially correct
29 Partially correct 362 ms 456 KB Output is partially correct
30 Partially correct 407 ms 336 KB Output is partially correct
31 Partially correct 321 ms 452 KB Output is partially correct
32 Correct 16 ms 336 KB Output is correct
33 Correct 13 ms 336 KB Output is correct
34 Correct 40 ms 336 KB Output is correct
35 Correct 33 ms 464 KB Output is correct
36 Correct 36 ms 336 KB Output is correct
37 Partially correct 287 ms 400 KB Output is partially correct
38 Partially correct 220 ms 436 KB Output is partially correct
39 Partially correct 206 ms 384 KB Output is partially correct
40 Partially correct 382 ms 400 KB Output is partially correct
41 Partially correct 233 ms 448 KB Output is partially correct
42 Partially correct 299 ms 516 KB Output is partially correct
43 Partially correct 817 ms 908 KB Output is partially correct
44 Partially correct 829 ms 948 KB Output is partially correct
45 Partially correct 894 ms 956 KB Output is partially correct
46 Partially correct 775 ms 1068 KB Output is partially correct
47 Execution timed out 1098 ms 896 KB Time limit exceeded
48 Halted 0 ms 0 KB -