Submission #1050121

# Submission time Handle Problem Language Result Execution time Memory
1050121 2024-08-09T07:34:04 Z phoenix Longest Trip (IOI23_longesttrip) C++17
60 / 100
743 ms 1216 KB
#include "longesttrip.h"
#include <bits/stdc++.h>

using namespace std;

#ifndef LOCAL
#define cerr if (false) cout
#endif

struct info {
    vector<int> A;
    vector<int> B;
    vector<int> ord;
};

int N;

bool g[256][256];

bool are_con(vector<int> a, vector<int> b) {
    for (int x : a) {
        for (int y : b) {
            if (g[x][y]) 
                return true;
        }    
    }
    return false;
} 

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

vector<int> operator + (vector<int> a, vector<int> b) {
    for (int c : b) a.push_back(c);
    return a;
} 

void print(vector<int> a) {
    cerr << '{';
    for (int c : a) 
        cerr << c << ' ';
    cerr << '}';
    cerr << '\n';
}

vector<int> fin(vector<int> V, int x) {
    for (int i = 0; i < (int)V.size() - 1; i++) {
        if (V[i] == x) swap(V[i], V[i + 1]); 
    }
    assert(V.back() == x);
    return V;
}

vector<int> beg(vector<int> V, int x) {
    for (int i = (int)V.size() - 1; i > 0; i--) {
        if (V[i] == x) swap(V[i], V[i - 1]); 
    }
    assert(V[0] == x);
    return V;
}

vector<int> connect(vector<int> A, vector<int> B) {
    int st = -1;
    for (int c : B) {
        if (are_con({A.back()}, {c})) {
            st = c;
            break;
        }
    }
    assert(st != -1);
    return A + beg(B, st);
};

info recursion(vector<int> V) {
    if ((int)V.size() == 1) 
        return {V, {}, V};
    vector<int> A, B;
    vector<bool> us(N);
    int p = V[rnd() % (int)V.size()];
    cerr << "chosen vertex: " << p << '\n';
    for (int c : V) if (p != c) {
        if (!are_con({p}, {c})) {
            B.push_back(c);
            us[c] = true;
        }
    }
    if (B.empty()) {
        info res;
        cerr << p << " is a star\n";
        V.erase(find(V.begin(), V.end(), p));
        info m = recursion(V);
        if (m.ord.empty()) {
            res.ord = m.A + vector<int>{p} + m.B;
        } else {
            res.ord = m.ord;
            res.ord.push_back(p);
        }
        return res;
    }
    for (int c : V) {
        if (us[c]) continue;
        if (!are_con({c}, B)) {
            A.push_back(c);
            us[c] = true;
        }
    }
    cerr << "Division: \n";
    print(A);
    print(B);
    cerr << '\n';
    shuffle(A.begin(), A.end(), rnd);
    shuffle(B.begin(), B.end(), rnd);

    vector<int> v1;
    for (int c : V) 
        if (!us[c]) 
            v1.push_back(c);
    if (v1.empty()) 
        return {A, B, {}};

    info M = recursion(v1);
    info res;
    
    if (!M.ord.empty()) {
        res.ord = fin(A, p) + connect(M.ord, B);
        return res;
    }
    for (int it = 0; it < 2; it++) {
        if ((int)A.size() == 1) {
            res.ord = M.A + A + connect(M.B, B);
            return res;       
        }
        swap(A, B);
    }
    int c = A[(A[0] == p)], t = M.A[0];
    if (are_con({c}, {t})) {
        res.ord = fin(M.A, t) + beg(fin(A, p), c) + connect(M.B, B);
    } else {
        res.ord = fin(A, p) + connect(M.B, B) + beg(M.A, t);  
    }
    return res;
}


vector<int> longest_trip(int N_ins, int D) {
    N = N_ins;
    for (int i = 0; i < N; i++) for (int j = 0; j < N; j++)
        g[i][j] = 0;
    for (int i = 0; i < N; i++)
        for (int j = i + 1; j < N; j++) {
            g[i][j] = g[j][i] = are_connected({i}, {j});
        }
    
    vector<int> v(N);
    for (int i = 0; i < N; i++) v[i] = i;
    info res = recursion(v);
    if (res.ord.empty()) {
        if ((int)res.A.size() < (int)res.B.size())
            swap(res.A, res.B);
        return res.A;
    }
    return res.ord;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 162 ms 484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 18 ms 344 KB Output is correct
3 Correct 112 ms 344 KB Output is correct
4 Correct 300 ms 448 KB Output is correct
5 Correct 669 ms 732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 18 ms 344 KB Output is correct
3 Correct 90 ms 344 KB Output is correct
4 Correct 305 ms 592 KB Output is correct
5 Correct 731 ms 736 KB Output is correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 19 ms 344 KB Output is correct
8 Correct 115 ms 600 KB Output is correct
9 Correct 251 ms 344 KB Output is correct
10 Correct 687 ms 1068 KB Output is correct
11 Correct 671 ms 848 KB Output is correct
12 Correct 707 ms 768 KB Output is correct
13 Correct 693 ms 780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 19 ms 344 KB Output is correct
3 Correct 112 ms 344 KB Output is correct
4 Correct 340 ms 448 KB Output is correct
5 Correct 679 ms 736 KB Output is correct
6 Correct 5 ms 344 KB Output is correct
7 Correct 19 ms 344 KB Output is correct
8 Correct 117 ms 344 KB Output is correct
9 Correct 243 ms 680 KB Output is correct
10 Correct 708 ms 812 KB Output is correct
11 Correct 661 ms 780 KB Output is correct
12 Correct 695 ms 780 KB Output is correct
13 Correct 658 ms 776 KB Output is correct
14 Correct 8 ms 344 KB Output is correct
15 Correct 9 ms 344 KB Output is correct
16 Correct 32 ms 344 KB Output is correct
17 Correct 66 ms 344 KB Output is correct
18 Correct 122 ms 456 KB Output is correct
19 Correct 239 ms 1216 KB Output is correct
20 Correct 230 ms 468 KB Output is correct
21 Correct 648 ms 552 KB Output is correct
22 Correct 673 ms 792 KB Output is correct
23 Correct 659 ms 596 KB Output is correct
24 Correct 690 ms 592 KB Output is correct
25 Correct 9 ms 344 KB Output is correct
26 Correct 7 ms 344 KB Output is correct
27 Correct 24 ms 344 KB Output is correct
28 Correct 18 ms 344 KB Output is correct
29 Correct 18 ms 344 KB Output is correct
30 Correct 153 ms 344 KB Output is correct
31 Correct 159 ms 344 KB Output is correct
32 Correct 168 ms 344 KB Output is correct
33 Correct 274 ms 460 KB Output is correct
34 Correct 232 ms 592 KB Output is correct
35 Correct 260 ms 344 KB Output is correct
36 Correct 731 ms 480 KB Output is correct
37 Correct 689 ms 484 KB Output is correct
38 Correct 692 ms 592 KB Output is correct
39 Correct 647 ms 476 KB Output is correct
40 Correct 727 ms 480 KB Output is correct
41 Correct 721 ms 476 KB Output is correct
42 Correct 693 ms 484 KB Output is correct
43 Correct 691 ms 476 KB Output is correct
44 Correct 668 ms 484 KB Output is correct
45 Correct 6 ms 344 KB Output is correct
46 Correct 7 ms 344 KB Output is correct
47 Correct 28 ms 344 KB Output is correct
48 Correct 18 ms 344 KB Output is correct
49 Correct 21 ms 344 KB Output is correct
50 Correct 179 ms 344 KB Output is correct
51 Correct 158 ms 600 KB Output is correct
52 Correct 166 ms 452 KB Output is correct
53 Correct 243 ms 344 KB Output is correct
54 Correct 276 ms 596 KB Output is correct
55 Correct 243 ms 344 KB Output is correct
56 Correct 720 ms 480 KB Output is correct
57 Correct 697 ms 480 KB Output is correct
58 Correct 657 ms 484 KB Output is correct
59 Correct 743 ms 484 KB Output is correct
60 Correct 734 ms 480 KB Output is correct
61 Correct 652 ms 480 KB Output is correct
62 Correct 696 ms 344 KB Output is correct
63 Correct 657 ms 480 KB Output is correct
64 Correct 638 ms 484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 18 ms 344 KB Output is correct
3 Partially correct 114 ms 344 KB Output is partially correct
4 Partially correct 361 ms 448 KB Output is partially correct
5 Partially correct 690 ms 732 KB Output is partially correct
6 Correct 4 ms 344 KB Output is correct
7 Correct 18 ms 344 KB Output is correct
8 Partially correct 117 ms 344 KB Output is partially correct
9 Partially correct 271 ms 780 KB Output is partially correct
10 Partially correct 698 ms 988 KB Output is partially correct
11 Partially correct 706 ms 776 KB Output is partially correct
12 Partially correct 703 ms 776 KB Output is partially correct
13 Partially correct 696 ms 772 KB Output is partially correct
14 Correct 7 ms 344 KB Output is correct
15 Correct 9 ms 344 KB Output is correct
16 Correct 35 ms 456 KB Output is correct
17 Partially correct 67 ms 344 KB Output is partially correct
18 Partially correct 112 ms 344 KB Output is partially correct
19 Partially correct 195 ms 472 KB Output is partially correct
20 Partially correct 225 ms 720 KB Output is partially correct
21 Correct 10 ms 344 KB Output is correct
22 Correct 7 ms 344 KB Output is correct
23 Correct 20 ms 344 KB Output is correct
24 Correct 17 ms 340 KB Output is correct
25 Correct 22 ms 344 KB Output is correct
26 Partially correct 153 ms 344 KB Output is partially correct
27 Partially correct 162 ms 344 KB Output is partially correct
28 Partially correct 160 ms 344 KB Output is partially correct
29 Partially correct 257 ms 344 KB Output is partially correct
30 Partially correct 254 ms 592 KB Output is partially correct
31 Partially correct 253 ms 456 KB Output is partially correct
32 Correct 7 ms 344 KB Output is correct
33 Correct 7 ms 344 KB Output is correct
34 Correct 20 ms 344 KB Output is correct
35 Correct 16 ms 344 KB Output is correct
36 Correct 24 ms 344 KB Output is correct
37 Partially correct 147 ms 464 KB Output is partially correct
38 Partially correct 178 ms 344 KB Output is partially correct
39 Partially correct 150 ms 344 KB Output is partially correct
40 Partially correct 284 ms 444 KB Output is partially correct
41 Partially correct 262 ms 444 KB Output is partially correct
42 Partially correct 249 ms 344 KB Output is partially correct
43 Partially correct 700 ms 544 KB Output is partially correct
44 Partially correct 656 ms 556 KB Output is partially correct
45 Partially correct 701 ms 556 KB Output is partially correct
46 Partially correct 655 ms 792 KB Output is partially correct
47 Partially correct 664 ms 480 KB Output is partially correct
48 Partially correct 702 ms 476 KB Output is partially correct
49 Partially correct 715 ms 476 KB Output is partially correct
50 Partially correct 693 ms 480 KB Output is partially correct
51 Partially correct 647 ms 616 KB Output is partially correct
52 Partially correct 702 ms 480 KB Output is partially correct
53 Partially correct 666 ms 476 KB Output is partially correct
54 Partially correct 702 ms 480 KB Output is partially correct
55 Partially correct 723 ms 480 KB Output is partially correct
56 Partially correct 709 ms 484 KB Output is partially correct
57 Partially correct 708 ms 484 KB Output is partially correct
58 Partially correct 690 ms 476 KB Output is partially correct
59 Partially correct 651 ms 476 KB Output is partially correct
60 Partially correct 657 ms 592 KB Output is partially correct
61 Partially correct 664 ms 480 KB Output is partially correct
62 Partially correct 693 ms 592 KB Output is partially correct
63 Partially correct 688 ms 496 KB Output is partially correct
64 Partially correct 682 ms 492 KB Output is partially correct
65 Partially correct 653 ms 480 KB Output is partially correct
66 Partially correct 688 ms 476 KB Output is partially correct
67 Partially correct 705 ms 476 KB Output is partially correct
68 Partially correct 639 ms 596 KB Output is partially correct
69 Partially correct 689 ms 736 KB Output is partially correct
70 Partially correct 689 ms 480 KB Output is partially correct
71 Partially correct 725 ms 480 KB Output is partially correct
72 Partially correct 727 ms 480 KB Output is partially correct
73 Partially correct 662 ms 484 KB Output is partially correct
74 Partially correct 685 ms 484 KB Output is partially correct
75 Partially correct 667 ms 476 KB Output is partially correct
76 Partially correct 656 ms 480 KB Output is partially correct
77 Partially correct 561 ms 480 KB Output is partially correct
78 Partially correct 610 ms 476 KB Output is partially correct
79 Partially correct 617 ms 476 KB Output is partially correct
80 Partially correct 624 ms 492 KB Output is partially correct
81 Partially correct 676 ms 480 KB Output is partially correct
82 Partially correct 604 ms 484 KB Output is partially correct