Submission #1020179

# Submission time Handle Problem Language Result Execution time Memory
1020179 2024-07-11T16:22:57 Z bobbilyking Longest Trip (IOI23_longesttrip) C++17
60 / 100
962 ms 600 KB
#include "longesttrip.h"
using namespace std;
#include <bits/stdc++.h>

#define F(i, l, r) for (int i = (l); i < (r); ++i)
#define A(a) (a).begin(), (a).end()

bool edge[300][300];
int parent[300];
int find(int i) {
    return i == parent[i] ? i : parent[i] = find(parent[i]);
}

int GN;

bool query(int A, int B) {
    assert(min(A, B) >= 0 and max(A, B) < GN);
    return edge[A][B] | edge[B][A];
}

void verify(vector<int> v) {
    set<int> cum(A(v));
    F(i, 1, v.size()) assert(query(v[i-1], v[i]));
    assert(v.size() == cum.size());
    for (auto x: v) assert(x >= 0 and x < GN);
}

vector<int> longest_trip(int N, int D)
{
    GN = N;
    // okay we're just going to build the spanning tree straight up, fuck this shit. 
    iota(parent, parent + N, 0ll);
    memset(edge, 0, sizeof edge);
    
    F(i, 0, N) F(j, i + 1, N) {
        if (are_connected({i}, {j})) {
            parent[find(i)] = find(j);
            edge[i][j] = edge[j][i] = 1;
        }
    }

    F(i, 0, N) if (find(i) != find(0)) {
        assert(D == 1);
        vector<int> v[2];
        F(j, 0, N) {
            v[find(j) == find(0)].push_back(j);
            assert(find(j) == find(i) || find(j) == find(0));
        }
        if (v[0].size() < v[1].size()) swap(v[0], v[1]);

        verify(v[0]);
        assert(v[0].size() >= v[1].size());

        return v[0];
    }

    vector<int> hamil;

    auto gen = [&]() {
        set<int> v;
        F(i, 0, N) if (find(A(hamil), i) == hamil.end()) v.insert(i);
        return v;
    };

    if (query({0}, {1})) {
        hamil = {0, 1};
    } else if (query({1}, {2})) {
        hamil = {1, 2};
    } else {
        hamil = {0, 2};
    }

    while (hamil.size() != N) { // this will reach N, with the exception of one case 
        // cout << "HOW " << endl;
        verify(hamil);

        if (query({hamil[0]}, {hamil.back()})) {
            // its true that we have a hamiltonian CYCLE, so any rotation is valid. 
            // (well in the case of n=2 behaves like it for our purposes)
        } else {
            // Pick any node not in hamil, this must be connected to either the front or the back.
            auto i = *gen().begin();
            if (query({hamil[0]}, {i})) hamil.insert(hamil.begin(), i);
            else {
                assert(query(hamil.back(), i));
                hamil.push_back(i);
            }
            
            continue;
        }

        // Okay, we want to find *any* connection between hamil and others, if exists already. 
        auto bad = gen();

        bool seen_edge = false;
        F(___, 0, hamil.size()) {
            auto v = hamil.back(); hamil.pop_back(); hamil.insert(hamil.begin(), v);
            verify(hamil);
            for (auto y: bad) if (edge[v][y]) {
                hamil.insert(hamil.begin(), y);
                verify(hamil);
                seen_edge = true; 
                goto end;
            }
        }
        end: 

        assert(seen_edge); // we already handle the CC = 2 case up there in this brute force approach
    }
    
    verify(hamil);
    assert(hamil.size() == N);

    return hamil;
}

Compilation message

longesttrip.cpp: In function 'void verify(std::vector<int>)':
longesttrip.cpp:5:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define F(i, l, r) for (int i = (l); i < (r); ++i)
      |                                        ^
longesttrip.cpp:23:5: note: in expansion of macro 'F'
   23 |     F(i, 1, v.size()) assert(query(v[i-1], v[i]));
      |     ^
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:73:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   73 |     while (hamil.size() != N) { // this will reach N, with the exception of one case
      |            ~~~~~~~~~~~~~^~~~
longesttrip.cpp:5:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define F(i, l, r) for (int i = (l); i < (r); ++i)
      |                                        ^
longesttrip.cpp:96:9: note: in expansion of macro 'F'
   96 |         F(___, 0, hamil.size()) {
      |         ^
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from longesttrip.cpp:3:
longesttrip.cpp:112:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  112 |     assert(hamil.size() == N);
      |            ~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 205 ms 500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 344 KB Output is correct
2 Correct 23 ms 344 KB Output is correct
3 Correct 163 ms 528 KB Output is correct
4 Correct 425 ms 592 KB Output is correct
5 Correct 901 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 340 KB Output is correct
2 Correct 30 ms 344 KB Output is correct
3 Correct 147 ms 524 KB Output is correct
4 Correct 429 ms 500 KB Output is correct
5 Correct 881 ms 500 KB Output is correct
6 Correct 10 ms 344 KB Output is correct
7 Correct 24 ms 344 KB Output is correct
8 Correct 175 ms 600 KB Output is correct
9 Correct 338 ms 496 KB Output is correct
10 Correct 905 ms 500 KB Output is correct
11 Correct 928 ms 592 KB Output is correct
12 Correct 939 ms 504 KB Output is correct
13 Correct 915 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 344 KB Output is correct
2 Correct 21 ms 344 KB Output is correct
3 Correct 158 ms 344 KB Output is correct
4 Correct 409 ms 344 KB Output is correct
5 Correct 879 ms 504 KB Output is correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 23 ms 344 KB Output is correct
8 Correct 162 ms 344 KB Output is correct
9 Correct 358 ms 344 KB Output is correct
10 Correct 887 ms 504 KB Output is correct
11 Correct 893 ms 496 KB Output is correct
12 Correct 897 ms 500 KB Output is correct
13 Correct 896 ms 500 KB Output is correct
14 Correct 11 ms 344 KB Output is correct
15 Correct 16 ms 344 KB Output is correct
16 Correct 46 ms 344 KB Output is correct
17 Correct 78 ms 344 KB Output is correct
18 Correct 170 ms 600 KB Output is correct
19 Correct 328 ms 344 KB Output is correct
20 Correct 360 ms 344 KB Output is correct
21 Correct 844 ms 500 KB Output is correct
22 Correct 852 ms 504 KB Output is correct
23 Correct 865 ms 496 KB Output is correct
24 Correct 875 ms 504 KB Output is correct
25 Correct 11 ms 344 KB Output is correct
26 Correct 9 ms 344 KB Output is correct
27 Correct 23 ms 344 KB Output is correct
28 Correct 28 ms 344 KB Output is correct
29 Correct 22 ms 344 KB Output is correct
30 Correct 184 ms 344 KB Output is correct
31 Correct 199 ms 592 KB Output is correct
32 Correct 192 ms 344 KB Output is correct
33 Correct 323 ms 344 KB Output is correct
34 Correct 302 ms 596 KB Output is correct
35 Correct 283 ms 344 KB Output is correct
36 Correct 885 ms 500 KB Output is correct
37 Correct 875 ms 500 KB Output is correct
38 Correct 816 ms 500 KB Output is correct
39 Correct 870 ms 504 KB Output is correct
40 Correct 805 ms 596 KB Output is correct
41 Correct 820 ms 500 KB Output is correct
42 Correct 850 ms 592 KB Output is correct
43 Correct 833 ms 500 KB Output is correct
44 Correct 811 ms 592 KB Output is correct
45 Correct 9 ms 344 KB Output is correct
46 Correct 7 ms 344 KB Output is correct
47 Correct 22 ms 344 KB Output is correct
48 Correct 23 ms 344 KB Output is correct
49 Correct 31 ms 344 KB Output is correct
50 Correct 203 ms 344 KB Output is correct
51 Correct 184 ms 592 KB Output is correct
52 Correct 209 ms 344 KB Output is correct
53 Correct 320 ms 596 KB Output is correct
54 Correct 291 ms 500 KB Output is correct
55 Correct 317 ms 344 KB Output is correct
56 Correct 892 ms 500 KB Output is correct
57 Correct 872 ms 592 KB Output is correct
58 Correct 863 ms 500 KB Output is correct
59 Correct 889 ms 512 KB Output is correct
60 Correct 896 ms 512 KB Output is correct
61 Correct 896 ms 496 KB Output is correct
62 Correct 858 ms 500 KB Output is correct
63 Correct 835 ms 504 KB Output is correct
64 Correct 909 ms 496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 344 KB Output is correct
2 Correct 23 ms 344 KB Output is correct
3 Partially correct 170 ms 524 KB Output is partially correct
4 Partially correct 444 ms 500 KB Output is partially correct
5 Partially correct 871 ms 496 KB Output is partially correct
6 Correct 9 ms 344 KB Output is correct
7 Correct 19 ms 344 KB Output is correct
8 Partially correct 129 ms 600 KB Output is partially correct
9 Partially correct 355 ms 344 KB Output is partially correct
10 Partially correct 866 ms 536 KB Output is partially correct
11 Partially correct 880 ms 500 KB Output is partially correct
12 Partially correct 906 ms 504 KB Output is partially correct
13 Partially correct 930 ms 500 KB Output is partially correct
14 Correct 10 ms 344 KB Output is correct
15 Correct 11 ms 344 KB Output is correct
16 Correct 46 ms 344 KB Output is correct
17 Partially correct 102 ms 344 KB Output is partially correct
18 Partially correct 160 ms 344 KB Output is partially correct
19 Partially correct 329 ms 344 KB Output is partially correct
20 Partially correct 350 ms 500 KB Output is partially correct
21 Correct 9 ms 344 KB Output is correct
22 Correct 9 ms 344 KB Output is correct
23 Correct 22 ms 344 KB Output is correct
24 Correct 24 ms 344 KB Output is correct
25 Correct 29 ms 344 KB Output is correct
26 Partially correct 225 ms 344 KB Output is partially correct
27 Partially correct 211 ms 344 KB Output is partially correct
28 Partially correct 197 ms 344 KB Output is partially correct
29 Partially correct 334 ms 500 KB Output is partially correct
30 Partially correct 342 ms 592 KB Output is partially correct
31 Partially correct 312 ms 344 KB Output is partially correct
32 Correct 13 ms 344 KB Output is correct
33 Correct 11 ms 344 KB Output is correct
34 Correct 33 ms 344 KB Output is correct
35 Correct 24 ms 344 KB Output is correct
36 Correct 19 ms 340 KB Output is correct
37 Partially correct 184 ms 344 KB Output is partially correct
38 Partially correct 214 ms 520 KB Output is partially correct
39 Partially correct 222 ms 344 KB Output is partially correct
40 Partially correct 344 ms 512 KB Output is partially correct
41 Partially correct 324 ms 496 KB Output is partially correct
42 Partially correct 304 ms 344 KB Output is partially correct
43 Partially correct 884 ms 504 KB Output is partially correct
44 Partially correct 858 ms 496 KB Output is partially correct
45 Partially correct 914 ms 500 KB Output is partially correct
46 Partially correct 901 ms 500 KB Output is partially correct
47 Partially correct 867 ms 500 KB Output is partially correct
48 Partially correct 888 ms 500 KB Output is partially correct
49 Partially correct 884 ms 500 KB Output is partially correct
50 Partially correct 862 ms 500 KB Output is partially correct
51 Partially correct 844 ms 592 KB Output is partially correct
52 Partially correct 879 ms 496 KB Output is partially correct
53 Partially correct 849 ms 500 KB Output is partially correct
54 Partially correct 893 ms 500 KB Output is partially correct
55 Partially correct 832 ms 592 KB Output is partially correct
56 Partially correct 863 ms 500 KB Output is partially correct
57 Partially correct 871 ms 500 KB Output is partially correct
58 Partially correct 872 ms 500 KB Output is partially correct
59 Partially correct 851 ms 500 KB Output is partially correct
60 Partially correct 843 ms 504 KB Output is partially correct
61 Partially correct 847 ms 504 KB Output is partially correct
62 Partially correct 929 ms 496 KB Output is partially correct
63 Partially correct 962 ms 500 KB Output is partially correct
64 Partially correct 916 ms 500 KB Output is partially correct
65 Partially correct 913 ms 496 KB Output is partially correct
66 Partially correct 784 ms 500 KB Output is partially correct
67 Partially correct 851 ms 500 KB Output is partially correct
68 Partially correct 840 ms 596 KB Output is partially correct
69 Partially correct 850 ms 596 KB Output is partially correct
70 Partially correct 877 ms 596 KB Output is partially correct
71 Partially correct 872 ms 496 KB Output is partially correct
72 Partially correct 771 ms 496 KB Output is partially correct
73 Partially correct 892 ms 496 KB Output is partially correct
74 Partially correct 786 ms 500 KB Output is partially correct
75 Partially correct 863 ms 496 KB Output is partially correct
76 Partially correct 814 ms 500 KB Output is partially correct
77 Partially correct 852 ms 504 KB Output is partially correct
78 Partially correct 852 ms 496 KB Output is partially correct
79 Partially correct 825 ms 524 KB Output is partially correct
80 Partially correct 846 ms 500 KB Output is partially correct
81 Partially correct 905 ms 500 KB Output is partially correct
82 Partially correct 862 ms 508 KB Output is partially correct