Submission #1025138

#TimeUsernameProblemLanguageResultExecution timeMemory
1025138vjudge1Thousands Islands (IOI22_islands)C++17
55 / 100
29 ms18332 KiB
#include "islands.h"
#include <bits/stdc++.h>
#include <variant>

using namespace std;

#define rall(s) s.rbegin(), s.rend()
#define all(s) s.begin(), s.end()
#define sz(s) (int) s.size()
#define s second 
#define f first 

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int N = 3e5;

vector<pii> g[N], pr(N);
vector<int> was(N), ans;

bool dfs(int u, int x) {
    was[u] = 1;
    if (sz(g[u]) > 1 + (u > 0)) {
        for (auto [to, id]: g[u]) {
            if (id == x) continue;
            ans.push_back(id);
            ans.push_back(id ^ 1);
        }
        for (auto [to, id]: g[u]) {
            if (id == x) continue;
            ans.push_back(id ^ 1);
            ans.push_back(id);
        }
        return true;
    }
    for (auto [to, id]: g[u]) {
        if (id == x) continue;
        ans.push_back(id);
        bool is = dfs(to, id ^ 1);
        ans.push_back(id);
        return is;
    }
    return false;
}

vector<pii> s;

bool fnd(int u) {
    was[u] = 2;
    for (auto [to, id]: g[u]) {
        if (!was[to]) {
            s.push_back({id, u});
            ans.push_back(id);
            if (fnd(to)) return true;
            ans.pop_back();
            s.pop_back();
        }else if (was[to] == 2) {
            vector<int> h;
            while (u != to) {
                h.push_back(s.back().f);
                u = s.back().s;
                s.pop_back();
            }
            ans.push_back(id);
            reverse(all(h));
            for (int x: h) ans.push_back(x ^ 1);
            ans.push_back(id ^ 1);
            ans.push_back(id);
            reverse(all(h));
            for (int x: h) ans.push_back(x);
            ans.push_back(id ^ 1);
            for (int x: h) ans.push_back(x ^ 1);
            while (sz(s)) {
                ans.push_back(s.back().f);
                s.pop_back();
            }
            return true;
        }
    }
    was[u] = 1;
    return false;
}

variant<bool, vector<int>> find_journey(int n, int m, vector<int> u, vector<int> v) {
    bool okk = 1;
    for (int i = 0; i < m; i++) {
        if ((i & 1) && (u[i] != u[i - 1] || v[i] != v[i - 1])) okk = 0;
        g[u[i]].push_back({v[i], i});
    }
    if (n == 2) {
        if (sz(g[0]) > 1 && sz(g[1])) {
            vector<int> ans = {g[0][0].s, g[1][0].s, g[0][1].s, g[0][0].s, g[1][0].s, g[0][1].s};
            return ans;
        }
        return false;
    }
    if (okk) {
        if (fnd(0)) return ans;
        return false;
    }
    if (n <= 400) {
        int a[n][n], ok = 1;
        for (int i = 0; i < n; i++) {
            if (sz(g[i]) != n - 1) ok = 0;
            for (auto [to, id]: g[i]) a[i][to] = id;
        }
        if (ok) {
            vector<int> ans = {a[0][1], a[1][2], a[2][0], a[0][2], a[2][1], a[1][0], a[2][0], a[1][2], a[0][1], a[1][0], a[2][1], a[0][2]};
            return ans;
        }
    }
    if (dfs(0, -1)) return ans;
    return false;
}
#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...