Submission #757617

#TimeUsernameProblemLanguageResultExecution timeMemory
757617boris_mihovThousands Islands (IOI22_islands)C++17
5 / 100
35 ms6772 KiB
#include "islands.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <variant>
#include <vector>

typedef long long llong;
const int MAXN = 100000 + 10;
const int INF  = 1e9;

int n, m;
int vis[MAXN];
std::vector <int> cycle;
std::vector <std::pair <int,int>> g[MAXN];

bool findCycleFirst(int node, int par)
{
    vis[node] = 1;
    for (const auto &[x, idx] : g[node])
    {
        if (par == idx)
        {
            continue;
        }

        if (x == 1)
        {
            cycle.push_back(idx);
            return true;
        }

        if (vis[x])
        {
            continue;
        }

        if (findCycleFirst(x, idx))
        {
            cycle.push_back(idx);
            return true;
        }
    }

    return false;
}

std::variant <bool, std::vector<int>> find_journey(int N, int M, std::vector <int> U, std::vector <int> V)
{
    n = N; m = M;
    bool isFirst = !(m & 1);
    bool isSecond = !(m & 1);
    for (int i = 0 ; i < m ; ++i)
    {
        U[i]++;
        V[i]++;
        if (i & 1)
        {
            isFirst &= (U[i] == V[i - 1] && V[i] == U[i - 1]);
            isSecond &= (U[i] == U[i - 1] && U[i] == U[i - 1]);
        }
    }

    if (n == 2)
    {
        int cntOne = 0;
        int cntTwo = 0;
        std::vector <int> one;
        std::vector <int> two;
        for (int i = 0 ; i < m ; ++i)
        {
            if (U[i] == 1)
            {
                cntOne++;
                one.push_back(i);
            } else
            {
                cntTwo++;
                two.push_back(i);
            }
        }

        if (cntOne >= 2 && cntTwo >= 1)
        {
            return std::vector <int> ({one[0], two[0], one[1], one[0], two[0], one[1]});
        }

        return false;
    }

    if (isFirst)
    {
        for (int i = 0 ; i < m ; i += 2)
        {
            g[U[i]].push_back({V[i], i});
            g[U[i + 1]].push_back({V[i + 1], i + 1});
        }

        bool isCycle = findCycleFirst(1, 0);
        if (!isCycle)
        {
            return false;
        }

        std::vector <int> ans;
        std::reverse(cycle.begin(), cycle.end());
        for (const int &i : cycle)
        {
            ans.push_back(i);
        }

        std::reverse(cycle.begin(), cycle.end());
        for (const int &i : cycle)
        {
            ans.push_back(i ^ 1);
        }

        for (const int &i : cycle)
        {
            ans.push_back(i);
        }

        std::reverse(cycle.begin(), cycle.end());
        for (const int &i : cycle)
        {
            ans.push_back(i ^ 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...