Submission #980230

#TimeUsernameProblemLanguageResultExecution timeMemory
980230vjudge1Longest Trip (IOI23_longesttrip)C++17
25 / 100
16 ms1112 KiB
#include "longesttrip.h"
#include <bits/stdc++.h>

#include <vector>
#define MP make_pair
#define f first
#define s second
typedef long long ll;
using namespace std;

mt19937_64 rnd(286);

const int INF = (int)1e6;

bool conectados(int A, int B) {
    vector<int> a,b;
    a.push_back(A);
    b.push_back(B);
    return are_connected(a,b);
}

struct linea{
    deque <int> lin;
    linea(){}
    linea(int A) {
        lin.push_front(A);
    }
    void unir(linea B) {
        while (!B.lin.empty()) {
            lin.push_front(B.lin.front());
            B.lin.pop_front();
        }
        return;
    }
};

vector <int> base(int N) {
    vector <int> ans;
    ans.push_back(0);
    if (conectados(0, 1)) ans.push_back(1);
    return ans;
}

std::vector<int> longest_trip(int N, int D) {
    if (N <= 2) return 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();
        if (conectados(A.lin.front(), B.lin.front())) {
            A.unir(B);
            V.push_back(A);
            V.push_back(C);
        }
        else if (conectados(A.lin.front(), C.lin.front())) {
            A.unir(C);
            V.push_back(A);
            V.push_back(B);
        }
        else {
            B.unir(C);
            V.push_back(B);
            V.push_back(A);
        }
    }
    if (V[0].lin.size() > V[1].lin.size()) swap(V[0], V[1]);
    while (!V[1].lin.empty()) {
        ans.push_back(V[1].lin.front());
        V[1].lin.pop_front();
    }
    return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...