답안 #1019621

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1019621 2024-07-11T05:37:21 Z bobbilyking 가장 긴 여행 (IOI23_longesttrip) C++17
5 / 100
11 ms 672 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;
            parent[find(x)] = find(y);
        }
    }

    // cout << "FUCK " << endl;

    return res; 
}

vector<int> longest_trip(int N, int D)
{
    iota(parent, parent + N, 0ll);
    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 {
        edge[0][2] = edge[2][0] = 1; parent[0] = 2;
        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;
        bool can_do_any = false;

        if (query({hamil[0]}, {hamil.back()})) {
            can_do_any = true; // its true that we have a hamiltonian CYCLE, so 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();

            // cout << "HAHA " << endl;
            if (query({hamil[0]}, {i})) hamil.insert(hamil.begin(), i);
            else {
                edge[hamil.back()][i] = edge[i][hamil.back()] = 1;
                parent[find(i)] = find(hamil.back());
                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: if (seen_edge) continue;

        // Okay, now we want to do the "pick one from end, and two disjoint components" strategy.
        while (true) {
            int state = 0;
            for (auto x: bad) for (auto y: bad) if (find(x) != find(y)) {
                if (query({x}, {y})) {
                    state = 1; goto end2; // we found something good 

                } else { // one MUST be connected to one of the endpoints
                    state = 2; 
                    if (query({x}, {hamil[0]})) {
                        hamil.insert(hamil.begin(), x);
                    } else {
                        edge[hamil.back()][x] = edge[x][hamil.back()] = 1;
                        parent[find(hamil.back())] = find(x);
                        hamil.push_back(x);
                    }
                    goto end2;
                }
            }
            end2:
            if (state == 1) continue;
            if (state == 2) break; 

            // OTHERWISE, RARE ONCE IN AN ITERATION CASE: WE ASK IF THERE'S ANY CONNECTION ACROSS BOTH COMPS.

            // IF NOT, THEN INSTANTLY KNOW TWO K_N COMPS.

            if (!query(hamil, vector<int>(A(bad)))) {
                if (hamil.size() < bad.size()) {
                    hamil = vector<int>(A(bad));
                }
                return hamil;
            }
            
            // ELSE, FIND ANY SPANNING EDGE WITH BSEARCH, TAKE IT. BREAK, WE WILL FIND THE SPANNING EDGE AGAIN NEXT LOOP. 

            vector<int> cur(A(bad));
            while (cur.size() > 1) {
                vector<int> shit(cur.begin(), cur.begin() + cur.size()/2);
                if (query(hamil, shit)) cur = shit;
                else cur = vector<int>(cur.begin() + cur.size() / 2, cur.end());
            }

            int x = cur[0];

            cur = hamil;
            while (cur.size() > 1) {
                vector<int> shit(cur.begin(), cur.begin() + cur.size()/2);
                if (query({x}, shit)) cur = shit;
                else cur = vector<int>(cur.begin() + cur.size() / 2, cur.end());
            }

            int y = cur[0];

            edge[x][y] = edge[y][x] = 1;
            break;
        }

        // cout << "WHILE TRUING " << endl;
    }


    return hamil;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:61:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   61 |     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:87:9: note: in expansion of macro 'F'
   87 |         F(i, 0, hamil.size()) for (auto y: bad) if (edge[hamil[i]][y]) {
      |         ^
longesttrip.cpp:64:14: warning: variable 'can_do_any' set but not used [-Wunused-but-set-variable]
   64 |         bool can_do_any = false;
      |              ^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Incorrect
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 2 ms 344 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 11 ms 672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 3 ms 344 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 10 ms 600 KB Output is correct
6 Incorrect 0 ms 344 KB Incorrect
7 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 2 ms 344 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 10 ms 600 KB Output is correct
6 Incorrect 0 ms 344 KB Incorrect
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
3 Correct 2 ms 344 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 10 ms 600 KB Output is correct
6 Incorrect 0 ms 344 KB Incorrect
7 Halted 0 ms 0 KB -