Submission #980328

# Submission time Handle Problem Language Result Execution time Memory
980328 2024-05-12T05:06:27 Z vjudge1 Longest Trip (IOI23_longesttrip) C++17
85 / 100
17 ms 1112 KB
#include "longesttrip.h"
#include <bits/stdc++.h>

#include <vector>
#define MP make_pair
#define f first
#define s second
#define mid ((l+r)>>1)
typedef long long ll;
using namespace std;

mt19937_64 rnd(33);

const int INF = (int)1e6;

vector <int> auxa,auxb;

bool conectados(int A, int B) {
    auxa.clear(); auxb.clear();
    auxa.push_back(A);
    auxb.push_back(B);
    return are_connected(auxa,auxb);
}

struct linea{
    deque <int> lin;
    linea(){}
    linea(int A) {
        lin.push_front(A);
    }
    void unir(linea &B, int NB, int NA) {
        if (NA == lin.front() && NB == B.lin.front()) {
            while (!B.lin.empty()) {
                lin.push_front(B.lin.front());
                B.lin.pop_front();
            }
            return;
        }
        if (NA == lin.front() && NB == B.lin.back()) {
            while (!B.lin.empty()) {
                lin.push_front(B.lin.back());
                B.lin.pop_back();
            }
            return;
        }
        if (NA == lin.back() && NB == B.lin.front()) {
            while (!B.lin.empty()) {
                lin.push_back(B.lin.front());
                B.lin.pop_front();
            }
            return;
        }
        if (NA == lin.back() && NB == B.lin.back()) {
            while (!B.lin.empty()) {
                lin.push_back(B.lin.back());
                B.lin.pop_back();
            }
            return;
        }
        return;
    }
};

vector <int> caso_base(int N) {
    auxa.clear();
    auxa.push_back(0);
    if (conectados(0, 1)) auxa.push_back(1);
    return auxa;
}

void dfs(int nodo, vector <bool> &vis, vector <vector<int>> &g, vector <int> &ans, int anc1, int anc2) {
    ans.push_back(nodo);
    if (ans.size() > 1) {
        anc1 = -1;
        anc2 = -1;
    }
    vis[nodo] = true;
    for (auto &it : g[nodo]) {
        if (vis[it] || it == anc1 || it == anc2) continue;
        dfs(it, vis, g, ans, anc1, anc2);
    }
    return;
}

int bin(int l, int r, vector <int> &ar, vector <int> &bus) {
    if (l == r) return ar[l];
    auxa.clear();
    for (int i = l; i <= mid; i++) auxa.push_back(ar[i]);
    if (are_connected(auxa, bus)) return bin(l, mid, ar, bus);
    else return bin(mid + 1, r, ar, bus);
}

void randomiza(linea &A, linea &B, linea &C) {
    vector <linea> vv;
    vv.push_back(A);
    vv.push_back(B);
    vv.push_back(C);
    shuffle(vv.begin(), vv.end(), rnd);
    A = vv[0];
    B = vv[1];
    C = vv[2];
    return;
}

std::vector<int> longest_trip(int N, int D) {
    if (N <= 2) return caso_base(N);
    vector <linea> V;
    vector <int> ans;
    for (int i = 0; i < N; i++)
        V.push_back(linea(i));
    shuffle(V.begin(),V.end(),rnd);
    while (V.size() >= 3) {
        linea A = V.back();
        V.pop_back();
        linea B = V.back();
        V.pop_back();
        linea C = V.back();
        V.pop_back();
        randomiza(A, B, C);
        if (conectados(A.lin.front(), B.lin.front())) {
            A.unir(B, B.lin.front(), A.lin.front());
            V.push_back(A);
            V.push_back(C);
        }
        else if (conectados(A.lin.front(), C.lin.front())) {
            A.unir(C, C.lin.front(), A.lin.front());
            V.push_back(A);
            V.push_back(B);
        }
        else {
            B.unir(C, C.lin.front(), B.lin.front());
            V.push_back(B);
            V.push_back(A);
        }
    }

    linea A = V[0], B = V[1];
    vector <int> VA, VB;
    while (!V[1].lin.empty()) {
        VB.push_back(V[1].lin.front());
        V[1].lin.pop_front();
    }
    while (!V[0].lin.empty()) {
        VA.push_back(V[0].lin.front());
        V[0].lin.pop_front();
    }

    if (!are_connected(VA,VB)) {
        if (VA.size() > VB.size()) return VA;
        return VB;
    }

    V[0] = A; V[1] = B;

    bool c1 = false, c2 = false, c3 = false, c4 = false;
    c1 = conectados(V[0].lin.front(), V[1].lin.front());
    c2 = conectados(V[0].lin.front(), V[1].lin.back());
    c3 = conectados(V[0].lin.back(), V[1].lin.back());
    c4 = conectados(V[0].lin.back(), V[1].lin.front());
    if (c1 || c2 || c3 || c4) {
        if (c1) V[1].unir(V[0], V[0].lin.front(), V[1].lin.front());
        else if (c2) V[1].unir(V[0], V[0].lin.front(), V[1].lin.back());
        else if (c3) V[1].unir(V[0], V[0].lin.back(), V[1].lin.back());
        else if (c4) V[1].unir(V[0], V[0].lin.back(), V[1].lin.front());

        while (!V[1].lin.empty()) {
            ans.push_back(V[1].lin.front());
            V[1].lin.pop_front();
        }
        return ans;
    }

    int ancla = bin(0, VA.size() - 1, VA, VB);
    vector <int> vancla;
    vancla.push_back(ancla);
    int ancla2 = bin(0, VB.size() - 1, VB, vancla);

    vector <vector<int>> g(N + 2);
    vector <bool> vis(N + 2);
    for (int i = 0; i < N + 2; i++)
        vis[i] = false;

    for (int i = 0; i < VA.size() - 1; i++) {
        g[VA[i]].push_back(VA[i + 1]);
        g[VA[i + 1]].push_back(VA[i]);
    }
    for (int i = 0; i < VB.size() - 1; i++) {
        g[VB[i]].push_back(VB[i + 1]);
        g[VB[i + 1]].push_back(VB[i]);
    }
    g[VA[VA.size() - 1]].push_back(VA[0]);
    g[VA[0]].push_back(VA[VA.size() - 1]);

    g[VB[VB.size() - 1]].push_back(VB[0]);
    g[VB[0]].push_back(VB[VB.size() - 1]);

    g[ancla].push_back(ancla2);
    g[ancla2].push_back(ancla);

    if (VA.size() != 1) {
        for (auto &it : g[ancla]) {
            if (it != ancla2) {
                dfs(it, vis, g, ans, ancla, ancla2);
                break;
            }
        }
    }
    else {
        for (auto &it : g[ancla2]) {
            if (it != ancla) {
                dfs(it, vis, g, ans, ancla, ancla2);
                break;
            }
        }
    }
    return ans;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:183:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  183 |     for (int i = 0; i < VA.size() - 1; i++) {
      |                     ~~^~~~~~~~~~~~~~~
longesttrip.cpp:187:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  187 |     for (int i = 0; i < VB.size() - 1; i++) {
      |                     ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 2 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 344 KB Output is correct
2 Correct 9 ms 344 KB Output is correct
3 Correct 10 ms 912 KB Output is correct
4 Correct 7 ms 344 KB Output is correct
5 Correct 7 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 344 KB Output is correct
2 Correct 11 ms 344 KB Output is correct
3 Correct 8 ms 600 KB Output is correct
4 Correct 11 ms 560 KB Output is correct
5 Correct 9 ms 1112 KB Output is correct
6 Correct 16 ms 344 KB Output is correct
7 Correct 11 ms 344 KB Output is correct
8 Correct 8 ms 500 KB Output is correct
9 Correct 7 ms 600 KB Output is correct
10 Correct 7 ms 936 KB Output is correct
11 Correct 7 ms 856 KB Output is correct
12 Correct 8 ms 940 KB Output is correct
13 Correct 7 ms 680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 344 KB Output is correct
2 Correct 8 ms 344 KB Output is correct
3 Correct 8 ms 600 KB Output is correct
4 Correct 9 ms 600 KB Output is correct
5 Correct 8 ms 1112 KB Output is correct
6 Correct 14 ms 344 KB Output is correct
7 Correct 9 ms 344 KB Output is correct
8 Correct 8 ms 600 KB Output is correct
9 Correct 7 ms 600 KB Output is correct
10 Correct 7 ms 860 KB Output is correct
11 Correct 8 ms 856 KB Output is correct
12 Correct 8 ms 856 KB Output is correct
13 Correct 8 ms 852 KB Output is correct
14 Correct 7 ms 344 KB Output is correct
15 Correct 10 ms 344 KB Output is correct
16 Correct 11 ms 344 KB Output is correct
17 Correct 8 ms 600 KB Output is correct
18 Correct 8 ms 500 KB Output is correct
19 Correct 9 ms 548 KB Output is correct
20 Correct 7 ms 344 KB Output is correct
21 Correct 9 ms 936 KB Output is correct
22 Correct 7 ms 856 KB Output is correct
23 Correct 8 ms 936 KB Output is correct
24 Correct 8 ms 856 KB Output is correct
25 Correct 8 ms 344 KB Output is correct
26 Correct 7 ms 344 KB Output is correct
27 Correct 9 ms 344 KB Output is correct
28 Correct 8 ms 344 KB Output is correct
29 Correct 9 ms 344 KB Output is correct
30 Correct 9 ms 504 KB Output is correct
31 Correct 13 ms 508 KB Output is correct
32 Correct 10 ms 500 KB Output is correct
33 Correct 9 ms 600 KB Output is correct
34 Correct 9 ms 600 KB Output is correct
35 Correct 9 ms 600 KB Output is correct
36 Correct 8 ms 688 KB Output is correct
37 Correct 9 ms 856 KB Output is correct
38 Correct 10 ms 1112 KB Output is correct
39 Correct 10 ms 688 KB Output is correct
40 Correct 10 ms 856 KB Output is correct
41 Correct 10 ms 600 KB Output is correct
42 Correct 9 ms 688 KB Output is correct
43 Correct 9 ms 680 KB Output is correct
44 Correct 9 ms 856 KB Output is correct
45 Correct 15 ms 344 KB Output is correct
46 Correct 12 ms 344 KB Output is correct
47 Correct 14 ms 600 KB Output is correct
48 Correct 13 ms 460 KB Output is correct
49 Correct 15 ms 452 KB Output is correct
50 Correct 9 ms 508 KB Output is correct
51 Correct 11 ms 500 KB Output is correct
52 Correct 13 ms 512 KB Output is correct
53 Correct 8 ms 856 KB Output is correct
54 Correct 9 ms 344 KB Output is correct
55 Correct 11 ms 808 KB Output is correct
56 Correct 9 ms 940 KB Output is correct
57 Correct 11 ms 1112 KB Output is correct
58 Correct 10 ms 940 KB Output is correct
59 Correct 10 ms 856 KB Output is correct
60 Correct 11 ms 940 KB Output is correct
61 Correct 10 ms 688 KB Output is correct
62 Correct 10 ms 1108 KB Output is correct
63 Correct 12 ms 680 KB Output is correct
64 Correct 9 ms 1112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 344 KB Output is correct
2 Correct 9 ms 344 KB Output is correct
3 Correct 7 ms 856 KB Output is correct
4 Correct 6 ms 344 KB Output is correct
5 Correct 6 ms 684 KB Output is correct
6 Correct 14 ms 344 KB Output is correct
7 Correct 8 ms 344 KB Output is correct
8 Correct 7 ms 344 KB Output is correct
9 Correct 8 ms 804 KB Output is correct
10 Correct 7 ms 1112 KB Output is correct
11 Correct 7 ms 1112 KB Output is correct
12 Correct 6 ms 940 KB Output is correct
13 Correct 7 ms 940 KB Output is correct
14 Correct 6 ms 344 KB Output is correct
15 Correct 8 ms 344 KB Output is correct
16 Correct 9 ms 344 KB Output is correct
17 Correct 8 ms 600 KB Output is correct
18 Correct 7 ms 600 KB Output is correct
19 Correct 7 ms 600 KB Output is correct
20 Correct 7 ms 344 KB Output is correct
21 Correct 9 ms 344 KB Output is correct
22 Correct 7 ms 344 KB Output is correct
23 Correct 7 ms 344 KB Output is correct
24 Correct 10 ms 344 KB Output is correct
25 Correct 7 ms 344 KB Output is correct
26 Correct 8 ms 504 KB Output is correct
27 Correct 10 ms 508 KB Output is correct
28 Correct 9 ms 508 KB Output is correct
29 Correct 8 ms 344 KB Output is correct
30 Correct 9 ms 600 KB Output is correct
31 Correct 10 ms 600 KB Output is correct
32 Correct 11 ms 344 KB Output is correct
33 Correct 17 ms 344 KB Output is correct
34 Correct 10 ms 600 KB Output is correct
35 Correct 10 ms 600 KB Output is correct
36 Correct 9 ms 600 KB Output is correct
37 Correct 8 ms 504 KB Output is correct
38 Correct 10 ms 756 KB Output is correct
39 Correct 13 ms 504 KB Output is correct
40 Correct 8 ms 1112 KB Output is correct
41 Correct 10 ms 344 KB Output is correct
42 Correct 11 ms 808 KB Output is correct
43 Correct 9 ms 856 KB Output is correct
44 Correct 7 ms 856 KB Output is correct
45 Correct 7 ms 860 KB Output is correct
46 Correct 8 ms 1112 KB Output is correct
47 Correct 8 ms 692 KB Output is correct
48 Partially correct 10 ms 856 KB Output is partially correct
49 Partially correct 10 ms 856 KB Output is partially correct
50 Partially correct 9 ms 680 KB Output is partially correct
51 Partially correct 9 ms 684 KB Output is partially correct
52 Partially correct 9 ms 600 KB Output is partially correct
53 Partially correct 9 ms 856 KB Output is partially correct
54 Partially correct 12 ms 856 KB Output is partially correct
55 Partially correct 10 ms 852 KB Output is partially correct
56 Partially correct 10 ms 856 KB Output is partially correct
57 Partially correct 10 ms 684 KB Output is partially correct
58 Partially correct 10 ms 856 KB Output is partially correct
59 Partially correct 10 ms 856 KB Output is partially correct
60 Partially correct 9 ms 856 KB Output is partially correct
61 Partially correct 10 ms 856 KB Output is partially correct
62 Partially correct 10 ms 856 KB Output is partially correct
63 Partially correct 12 ms 872 KB Output is partially correct
64 Partially correct 11 ms 880 KB Output is partially correct
65 Partially correct 10 ms 856 KB Output is partially correct
66 Partially correct 12 ms 872 KB Output is partially correct
67 Partially correct 11 ms 700 KB Output is partially correct
68 Partially correct 10 ms 856 KB Output is partially correct
69 Partially correct 14 ms 856 KB Output is partially correct
70 Partially correct 11 ms 856 KB Output is partially correct
71 Partially correct 9 ms 1112 KB Output is partially correct
72 Partially correct 10 ms 1112 KB Output is partially correct
73 Partially correct 10 ms 856 KB Output is partially correct
74 Partially correct 10 ms 856 KB Output is partially correct
75 Partially correct 10 ms 856 KB Output is partially correct
76 Partially correct 10 ms 680 KB Output is partially correct
77 Partially correct 9 ms 684 KB Output is partially correct
78 Partially correct 11 ms 680 KB Output is partially correct
79 Partially correct 11 ms 856 KB Output is partially correct
80 Partially correct 13 ms 956 KB Output is partially correct
81 Partially correct 11 ms 1108 KB Output is partially correct
82 Partially correct 10 ms 876 KB Output is partially correct