답안 #1020196

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1020196 2024-07-11T16:42:02 Z bobbilyking 가장 긴 여행 (IOI23_longesttrip) C++17
70 / 100
80 ms 1112 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]);
}

void add_edge(int A, int B) {
    edge[A][B] = edge[B][A] = 1;
    parent[find(A)] = find(B);
}
 
bool query(vector<int> A, vector<int> B) {
    // for (auto x: A ) cout << x << " "; cout << endl;
    // for (auto x: B ) cout << x << " "; cout << 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) {
            add_edge(A[0], B[0]);
        }
    }
 
    return res; 
}
 
vector<int> longest_trip(int N, int D)
{
    iota(parent, parent + N, 0ll);
    memset(edge, 0, sizeof edge);
 
    deque<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 {
        add_edge(0, 2);
        hamil = {0, 2};
    }
 
    while (hamil.size() != N) { // this will reach N, with the exception of one case 
        // for (auto x: hamil) cout << x << " "; cout << endl;;

        // Mandatory 1 query here.
        // If we go into other branch, good, we only need 2 queries at most.  

        if (query({hamil[0]}, {hamil.back()})) {
            // behaves as hamil cycle 
        } 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.push_front(i);
            else {
                add_edge(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;

        auto prev = hamil;
        F(i, 0, hamil.size()) {
            auto v = hamil.back(); hamil.pop_back(); hamil.push_front(v); 
            for (auto y: bad) if (edge[v][y]) {
                hamil.push_front(y);
                seen_edge = 1;
                goto end;
            }
        }
        end: if (seen_edge) continue;

        assert(prev == hamil);

        // cout << " ivaldf " << endl;
 
        // Okay, now we want to do the "pick one from end, and two disjoint components" strategy.
        while (true) {
            int state = 0;
            // Amortized shit; 
            // OOOH. But here it *can* overlap, we pay 1 mandatory per thing and 2 mandatory per amortized, so in total
            // 3N queries worst case... hmm...

            for (auto x: bad) for (auto y: bad) if (find(x) != find(y)) {
                // cout << "YA " << x << " " << y << endl;
                if (query({x}, {y})) {
                    // cout << "Shitty " << endl;
                    state = 1; goto end2; // incr spanning tree, okay.
 
                } else { // actually no edge between these, therefore one end MUST connect to hamil[0].
                    state = 2; 
                    // cout << " HOW THO " << x << " " << y << " " << hamil[0] << endl;
                    if (query({x}, {hamil[0]})) {
                        hamil.push_front(x);
                    } else {
                        add_edge(hamil[0], y);
                        hamil.push_front(y);
                    }
                    goto end2;
                }
            }
            end2:
            if (state == 1) continue;
            if (state == 2) break; 
 
            // Mandatory pay once, 2 * log(n) queries 

            // 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.

            // cout << "CHECKING KN " << endl;
 
            if (!query(vector<int>(A(hamil)), vector<int>(A(bad)))) {
                if (hamil.size() < bad.size()) {
                    return vector<int>(A(bad));
                }
                return vector<int>(A(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(vector<int>(A(hamil)), shit)) cur = shit;
                else cur = vector<int>(cur.begin() + cur.size() / 2, cur.end());
            }
 
            int x = cur[0];
 
            cur = vector<int>(A(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];

            add_edge(x, y);
            break;
        }
 
        // cout << "WHILE TRUING " << endl;
    }
 
 
    return vector<int>(A(hamil));
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:63:25: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   63 |     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::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define F(i, l, r) for (int i = (l); i < (r); ++i)
      |                                        ^
longesttrip.cpp:91:9: note: in expansion of macro 'F'
   91 |         F(i, 0, hamil.size()) {
      |         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 12 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 600 KB Output is correct
2 Correct 12 ms 600 KB Output is correct
3 Correct 13 ms 856 KB Output is correct
4 Correct 23 ms 600 KB Output is correct
5 Correct 58 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 600 KB Output is correct
2 Correct 12 ms 600 KB Output is correct
3 Correct 13 ms 600 KB Output is correct
4 Correct 22 ms 600 KB Output is correct
5 Correct 55 ms 600 KB Output is correct
6 Correct 9 ms 600 KB Output is correct
7 Correct 13 ms 600 KB Output is correct
8 Correct 14 ms 600 KB Output is correct
9 Correct 28 ms 600 KB Output is correct
10 Correct 54 ms 800 KB Output is correct
11 Correct 75 ms 848 KB Output is correct
12 Correct 54 ms 808 KB Output is correct
13 Correct 55 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 600 KB Output is correct
2 Correct 12 ms 600 KB Output is correct
3 Correct 14 ms 600 KB Output is correct
4 Correct 23 ms 600 KB Output is correct
5 Correct 56 ms 600 KB Output is correct
6 Correct 10 ms 600 KB Output is correct
7 Correct 17 ms 600 KB Output is correct
8 Correct 16 ms 600 KB Output is correct
9 Correct 18 ms 804 KB Output is correct
10 Correct 53 ms 804 KB Output is correct
11 Correct 68 ms 848 KB Output is correct
12 Correct 55 ms 848 KB Output is correct
13 Correct 80 ms 600 KB Output is correct
14 Correct 8 ms 600 KB Output is correct
15 Correct 9 ms 600 KB Output is correct
16 Correct 13 ms 600 KB Output is correct
17 Correct 16 ms 600 KB Output is correct
18 Correct 15 ms 596 KB Output is correct
19 Correct 19 ms 600 KB Output is correct
20 Correct 18 ms 600 KB Output is correct
21 Correct 48 ms 800 KB Output is correct
22 Correct 50 ms 796 KB Output is correct
23 Correct 63 ms 600 KB Output is correct
24 Correct 51 ms 808 KB Output is correct
25 Correct 13 ms 600 KB Output is correct
26 Correct 7 ms 600 KB Output is correct
27 Correct 12 ms 600 KB Output is correct
28 Correct 12 ms 600 KB Output is correct
29 Correct 12 ms 600 KB Output is correct
30 Correct 16 ms 600 KB Output is correct
31 Correct 16 ms 600 KB Output is correct
32 Correct 12 ms 600 KB Output is correct
33 Correct 19 ms 600 KB Output is correct
34 Correct 20 ms 600 KB Output is correct
35 Correct 22 ms 600 KB Output is correct
36 Correct 57 ms 600 KB Output is correct
37 Correct 58 ms 600 KB Output is correct
38 Correct 53 ms 808 KB Output is correct
39 Correct 49 ms 600 KB Output is correct
40 Correct 34 ms 600 KB Output is correct
41 Correct 34 ms 596 KB Output is correct
42 Correct 38 ms 600 KB Output is correct
43 Correct 35 ms 600 KB Output is correct
44 Correct 32 ms 600 KB Output is correct
45 Correct 11 ms 600 KB Output is correct
46 Correct 12 ms 600 KB Output is correct
47 Correct 18 ms 600 KB Output is correct
48 Correct 12 ms 600 KB Output is correct
49 Correct 12 ms 600 KB Output is correct
50 Correct 16 ms 600 KB Output is correct
51 Correct 28 ms 600 KB Output is correct
52 Correct 27 ms 600 KB Output is correct
53 Correct 18 ms 600 KB Output is correct
54 Correct 24 ms 600 KB Output is correct
55 Correct 20 ms 600 KB Output is correct
56 Correct 74 ms 784 KB Output is correct
57 Correct 61 ms 600 KB Output is correct
58 Correct 57 ms 600 KB Output is correct
59 Correct 51 ms 804 KB Output is correct
60 Correct 50 ms 600 KB Output is correct
61 Correct 42 ms 600 KB Output is correct
62 Correct 46 ms 808 KB Output is correct
63 Correct 43 ms 600 KB Output is correct
64 Correct 52 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 600 KB Output is correct
2 Correct 11 ms 600 KB Output is correct
3 Correct 16 ms 600 KB Output is correct
4 Correct 22 ms 600 KB Output is correct
5 Partially correct 56 ms 600 KB Output is partially correct
6 Correct 10 ms 600 KB Output is correct
7 Correct 12 ms 600 KB Output is correct
8 Correct 14 ms 604 KB Output is correct
9 Correct 17 ms 600 KB Output is correct
10 Partially correct 73 ms 820 KB Output is partially correct
11 Partially correct 53 ms 600 KB Output is partially correct
12 Partially correct 60 ms 600 KB Output is partially correct
13 Partially correct 54 ms 600 KB Output is partially correct
14 Correct 12 ms 600 KB Output is correct
15 Correct 9 ms 600 KB Output is correct
16 Correct 13 ms 600 KB Output is correct
17 Correct 19 ms 600 KB Output is correct
18 Correct 19 ms 792 KB Output is correct
19 Correct 18 ms 600 KB Output is correct
20 Correct 18 ms 852 KB Output is correct
21 Correct 9 ms 600 KB Output is correct
22 Correct 8 ms 600 KB Output is correct
23 Correct 14 ms 600 KB Output is correct
24 Correct 12 ms 600 KB Output is correct
25 Correct 12 ms 600 KB Output is correct
26 Correct 17 ms 600 KB Output is correct
27 Correct 16 ms 600 KB Output is correct
28 Correct 13 ms 600 KB Output is correct
29 Correct 20 ms 600 KB Output is correct
30 Correct 20 ms 600 KB Output is correct
31 Correct 25 ms 600 KB Output is correct
32 Correct 12 ms 600 KB Output is correct
33 Correct 11 ms 600 KB Output is correct
34 Correct 14 ms 600 KB Output is correct
35 Correct 13 ms 600 KB Output is correct
36 Correct 19 ms 600 KB Output is correct
37 Correct 16 ms 600 KB Output is correct
38 Correct 18 ms 800 KB Output is correct
39 Correct 17 ms 600 KB Output is correct
40 Correct 21 ms 600 KB Output is correct
41 Correct 22 ms 600 KB Output is correct
42 Correct 21 ms 600 KB Output is correct
43 Partially correct 52 ms 792 KB Output is partially correct
44 Partially correct 48 ms 600 KB Output is partially correct
45 Partially correct 49 ms 600 KB Output is partially correct
46 Partially correct 50 ms 808 KB Output is partially correct
47 Partially correct 58 ms 600 KB Output is partially correct
48 Partially correct 74 ms 600 KB Output is partially correct
49 Partially correct 65 ms 808 KB Output is partially correct
50 Partially correct 50 ms 600 KB Output is partially correct
51 Partially correct 34 ms 600 KB Output is partially correct
52 Partially correct 34 ms 600 KB Output is partially correct
53 Partially correct 42 ms 600 KB Output is partially correct
54 Partially correct 34 ms 600 KB Output is partially correct
55 Partially correct 35 ms 600 KB Output is partially correct
56 Partially correct 58 ms 600 KB Output is partially correct
57 Partially correct 70 ms 600 KB Output is partially correct
58 Partially correct 46 ms 600 KB Output is partially correct
59 Partially correct 34 ms 852 KB Output is partially correct
60 Partially correct 34 ms 804 KB Output is partially correct
61 Partially correct 36 ms 600 KB Output is partially correct
62 Partially correct 53 ms 788 KB Output is partially correct
63 Partially correct 65 ms 856 KB Output is partially correct
64 Partially correct 63 ms 600 KB Output is partially correct
65 Partially correct 64 ms 856 KB Output is partially correct
66 Partially correct 53 ms 600 KB Output is partially correct
67 Partially correct 40 ms 600 KB Output is partially correct
68 Partially correct 42 ms 600 KB Output is partially correct
69 Partially correct 48 ms 804 KB Output is partially correct
70 Partially correct 46 ms 600 KB Output is partially correct
71 Partially correct 58 ms 600 KB Output is partially correct
72 Partially correct 41 ms 812 KB Output is partially correct
73 Partially correct 47 ms 600 KB Output is partially correct
74 Partially correct 45 ms 1112 KB Output is partially correct
75 Partially correct 46 ms 600 KB Output is partially correct
76 Partially correct 48 ms 856 KB Output is partially correct
77 Partially correct 62 ms 600 KB Output is partially correct
78 Partially correct 58 ms 600 KB Output is partially correct
79 Partially correct 58 ms 600 KB Output is partially correct
80 Partially correct 59 ms 600 KB Output is partially correct
81 Partially correct 44 ms 600 KB Output is partially correct
82 Partially correct 46 ms 600 KB Output is partially correct