Submission #1019627

# Submission time Handle Problem Language Result Execution time Memory
1019627 2024-07-11T05:54:43 Z bobbilyking Longest Trip (IOI23_longesttrip) C++17
5 / 100
238 ms 724 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()

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

bool query(vector<int> A, vector<int> B) {
    // for (auto x: A) cout << x << endl;
    // for (auto x: B) cout << x << endl;
    
    if (A.size() == 1 and B.size() == 1) {
        if (edge[A[0]][B[0]]) return 1;
    }

    bool res = are_connected(A, B);

    if (res) {
        if (A.size() == 1 and B.size() == 1) {
            auto x = A[0], y = B[0];
            edge[x][y] = edge[y][x] = 1;
        }
    }

    return res; 
}

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

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

    vector<int> hamil;

    auto gen = [&]() {
        set<int> v;
        vector<bool> seen(N+1);
        for (auto x: hamil) seen[x] = 1;
        F(i, 0, N) if (!seen[i]) 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;
        bool is_two = hamil.size() == 2;

        if (query({hamil[0]}, {hamil.back()})) {
            // its true that we have a hamiltonian CYCLE, so any rotation is valid. 
        } 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 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(i, 0, hamil.size()) for (auto y: bad) if (edge[hamil[i]][y]) {
            // for (auto x: hamil) cout << x << " "; cout << endl;
            
            if (is_two) {
                // attach to the correct end
                if (i == 0) hamil.insert(hamil.begin(), y);
                else hamil.insert(hamil.end(), y);
            } else {
                hamil.insert(hamil.begin() + i, y);
                while (hamil.back() != y) { // really dumb rotate 
                    auto v = hamil.back(); hamil.pop_back(); hamil.insert(hamil.begin(), v);
                }
            }
            seen_edge = true; 
            
            // cout << "GOTO END " << endl;
            goto end;
        }
        end: 

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


    return hamil;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:70:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   70 |     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:90:9: note: in expansion of macro 'F'
   90 |         F(i, 0, hamil.size()) for (auto y: bad) if (edge[hamil[i]][y]) {
      |         ^
# Verdict Execution time Memory Grader output
1 Incorrect 0 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 3 ms 344 KB Output is correct
3 Correct 8 ms 344 KB Output is correct
4 Correct 43 ms 564 KB Output is correct
5 Correct 232 ms 716 KB Output is correct
# 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 8 ms 344 KB Output is correct
4 Correct 56 ms 572 KB Output is correct
5 Correct 238 ms 724 KB Output is correct
6 Incorrect 0 ms 344 KB Incorrect
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
3 Correct 8 ms 508 KB Output is correct
4 Correct 47 ms 572 KB Output is correct
5 Correct 214 ms 724 KB Output is correct
6 Incorrect 0 ms 344 KB Incorrect
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
3 Partially correct 12 ms 344 KB Output is partially correct
4 Partially correct 48 ms 592 KB Output is partially correct
5 Partially correct 225 ms 724 KB Output is partially correct
6 Incorrect 1 ms 344 KB Incorrect
7 Halted 0 ms 0 KB -