Submission #1182362

#TimeUsernameProblemLanguageResultExecution timeMemory
1182362PagodePaiva수천개의 섬 (IOI22_islands)C++20
0 / 100
65 ms35760 KiB
#include "islands.h"
#include<bits/stdc++.h>
#include <variant>
#include <vector>
#define arestas aresta

using namespace std;

const int N = 1010;
set <int> g[N];
int mark[N], pai[N];
vector <int> ans;
vector <int> aresta[N][N];
vector <int> topsort;

bool dfs(int v, int p){
    mark[v] = 1;
    pai[v] = p;
    for(auto x : g[v]){
        if(mark[x]){
            return true;
            vector <int> ciclo;
            int xx = v;
            while(xx != x){
                ciclo.push_back(xx);
                xx = pai[xx];
            }
            ciclo.push_back(x);
            vector <int> path;
            xx = x;
            while(xx != 0){
                path.push_back(xx);
                xx = pai[xx];
            }
            path.push_back(0);
            reverse(path.begin(), path.end());
            //exit(0);
            for(int i = 0;i+1 < path.size();i++){
                ans.push_back(arestas[path[i]][path[i+1]][0]);
            }
            reverse(ciclo.begin(), ciclo.end());
            int t = ciclo.size();
            for(int i = 0;i < ciclo.size();i++){
                ans.push_back(arestas[ciclo[i]][ciclo[(i+1)%t]][0]);
            }
            for(int i = 0;i < ciclo.size();i++){
                ans.push_back(arestas[ciclo[i]][ciclo[(i+1)%t]][1]);
            }
            for(int i = ciclo.size()-1;i > 0;i--){
                i++;
                ans.push_back(arestas[ciclo[((i-1)%t+t)%t]][ciclo[i%t]][0]);
                i--;
            }
            for(int i = ciclo.size()-1;i > 0;i--){
                i++;
                ans.push_back(arestas[ciclo[((i-1)%t+t)%t]][ciclo[i%t]][1]);
                i--;
            }
            t = path.size();
            for(int i = t-2;i > 0;i--){
                ans.push_back(arestas[path[i]][path[i+1]][0]);
            }
            //exit(0);
            return true;
        } 
        if(dfs(x, v)) return true;
    }
    return false;
}

int deg[N];
set <int> gc[N];
std::variant<bool, std::vector<int>> find_journey(int N, int M, std::vector<int> U, std::vector<int> V) {
    int n = N;
    for(int i = 0;i < M;i++){
        g[U[i]].insert(V[i]);
        gc[V[i]].insert(U[i]);
        aresta[U[i]][V[i]].push_back(i);
    } 
    stack <int> s;
    for(int i = 0;i < n;i++){
        deg[i] = g[i].size();
        if(deg[i] == 0) s.push(i);
    }
    while(!s.empty()){
        int v = s.top();
        s.pop();
        for(auto x : gc[v]){
            deg[x]--;
            if(deg[x] == 0) s.push(x);
        }
        topsort.push_back(v);
    }
    if(topsort.size() == n) return false;
    return ans;
    if(dfs(0, 0)) 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...