Submission #710710

#TimeUsernameProblemLanguageResultExecution timeMemory
710710Jarif_RahmanThousands Islands (IOI22_islands)C++17
19.25 / 100
273 ms24768 KiB
#include "islands.h"
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

int n, m, k;
vector<vector<int>> initial_graph;
vector<bool> reachable;

vector<multiset<int>> in(n), out(n);
set<pair<int, int>> st;

void dfs_reachability(int nd){
    if(reachable[nd]) return;
    reachable[nd] = 1;
    for(int x: initial_graph[nd]) dfs_reachability(x);
}

void remove_node(int nd){
    st.erase({out[nd].size(), nd});

    for(int x: in[nd]){
        st.erase({out[x].size(), x});
        out[x].erase(out[x].find(nd));
        st.insert({out[x].size(), x});
    }
    for(int x: out[nd]) in[x].erase(in[x].find(nd));
    out[nd].clear();
    in[nd].clear();
}

void preprocess(const vector<int>& U, const vector<int>& V){
    in.resize(n);
    out.resize(n);
    for(int i = 0; i < m; i++){
        if(!reachable[U[i]] || !reachable[V[i]]) continue;
        out[U[i]].insert(V[i]);
        in[V[i]].insert(U[i]);
    }

    for(int i = 0; i < n; i++){
        st.insert({out[i].size(), i});
    }

    while(!st.empty() && st.begin()->f == 0) remove_node(st.begin()->sc);
};


variant<bool, vector<int>> find_journey(int _n, int _m, vector<int> U, vector<int> V){
    n = _n, m = _m;

    initial_graph.assign(n, {});
    reachable.assign(n, 0);

    for(int i = 0; i < m; i++) initial_graph[U[i]].pb(V[i]);

    dfs_reachability(0);
    preprocess(U, V);

    set<int> cur = {0};

    bool ok = 0;
    while(!cur.empty()){
        for(int x: cur){
            if(out[x].size() >= 2) ok = 1;
        }
        if(ok) break;
        set<int> _cur;
        for(int x: cur) for(int y: out[x]) _cur.insert(y);
        for(int x: cur) remove_node(x);
        swap(cur, _cur);
    }

    return ok;
}
#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...