Submission #1236960

#TimeUsernameProblemLanguageResultExecution timeMemory
1236960countlessThousands Islands (IOI22_islands)C++20
8.40 / 100
21 ms4932 KiB
#include "islands.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

#define sp <<" "<<
#define endl "\n"

variant<bool, vector<int>> find_journey(int N, int M, vector<int> U, vector<int> V) {
    vector<vector<pair<int, int>>> adj(N), rev(N);
    for (int i = 0; i < M; i+=2) {
        adj[U[i]].emplace_back(V[i], i);
    }

    // st3 / st4
    // FIND ANY CYCLE. note that for st3/4 we can just topo sort no?
    vector<bool> vis(N), proc(N);
    bool found = false;
    int st = -1, en = -1;
    vector<pair<int, int>> par(N, make_pair(-1, -1));
    vector<int> path, cyc;

    auto dfs = [&](auto &&dfs, int u) -> void {
        if (found) return;
        vis[u] = proc[u] = true;

        for (auto &[v, x] : adj[u]) {
            if (vis[v] and proc[v]) {
                found = true;
                st = v;
                en = u;
                cyc.push_back(x);
                return;
            }

            if (!vis[v]) {
                par[v] = make_pair(u, x);
                dfs(dfs, v);
            }
        }

        proc[u] = false;
    };

    dfs(dfs, 0);

    if (!found) return false;

    int at = en;
    while (par[at].first != -1 and at != st) {
        cyc.push_back(par[at].second);
        at = par[at].first;
    }

    while (par[at].first != -1) {
        path.push_back(par[at].second);
        at = par[at].first;
    }

    // cerr << "Here" << endl;

    // subtask 3
    vector<int> ans;
    int n = cyc.size(), m = path.size();
    for (int i = m-1; i >= 0; i--) {
        ans.push_back(path[i]);
    }

    for (int i = n-1; i >= 0; i--) {
        ans.push_back(cyc[i]);
    }
    
    for (int i = n-1; i >= 0; i--) {
        ans.push_back(cyc[i]+1);
    }

    for (int i = 0; i < n; i++) {
        ans.push_back(cyc[i]);
    }

    for (int i = 0; i < n; i++) {
        ans.push_back(cyc[i]+1);
    }

    for (int i = 0; i < m; i++) {
        ans.push_back(path[i]);
    }

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