Submission #655373

#TimeUsernameProblemLanguageResultExecution timeMemory
655373LoboThousands Islands (IOI22_islands)C++17
100 / 100
196 ms30132 KiB
#include "islands.h"
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fr first
#define sc second
#define mp make_pair
#define all(x) x.begin(),x.end()

const int maxn = 2e5+10;

int n, m, Ug[maxn], Vg[maxn], grin[maxn], grout[maxn], atv[maxn], atvedg[maxn], pos[maxn];
vector<int> gin[maxn], gout[maxn], g[maxn];  
vector<int> ans;

variant<bool, vector<int>> find_journey(int N, int M, vector<int> U, vector<int> V) {
    n = N;
    m = M;
    for(int i = 0; i < m; i++) {
        Ug[i] = U[i];
        Vg[i] = V[i];
        gout[Ug[i]].pb(i);
        gin[Vg[i]].pb(i);
    }

    queue<int> qout0;
    for(int i = 0; i < m; i++) {
        grin[Vg[i]]++;
        grout[Ug[i]]++;
    }

    for(int i = 0; i < n; i++) {
        atv[i] = 1;
        if(grout[i] == 0) qout0.push(i);
    }
    int x = 0;
    while(x != -1) {
        while(qout0.size()) {
            int v = qout0.front();
            atv[v] = 0;
            qout0.pop();
            // delete v, I just need to delete the in edges
            for(auto id : gin[v]) {
                int u = Ug[id];
                grout[u]--;
                if(grout[u] == 0) {
                    qout0.push(u);
                }
            }
        }


        if(grout[x] == 0) {
            x = -1;
            ans.clear();
        }
        else if(grout[x] == 1) {
            //just use it
            // delete the out edge, them it will delete the in edges automatically
            for(auto id : gout[x]) {
                int v = Vg[id];
                if(atv[v]) {
                    grin[v]--;
                    grout[x]--;
                    qout0.push(x);
                    ans.pb(id);
                    x = v;
                    break;
                }
            }
        }
        else {
            int szant = ans.size();
            for(auto id : gout[x]) {
                int v = Vg[id];
                if(atv[v]) {
                    g[x].pb(id);
                    g[v].pb(id);
                    pos[id] = x;
                    if(g[x].size() == 2) break;
                }
            }
            for(int i = 0; i < n; i++) {
                if(i == x || atv[i] == 0) continue;
                for(auto id : gout[i]) {
                    int v = Vg[id];
                    if(atv[v]) {
                        g[i].pb(id);
                        g[v].pb(id);
                        pos[id] = i;
                        break;
                    }
                }
            }
            int u = x;
            int last = -1;
            int cnt = 0;
            int cntt = 0;
            while(!(u == x && last != -1 && cnt == 0)) {
                for(auto id : g[u]) {
                    if(u != pos[id] || id == last) continue;
                    int v;
                    if(u == Ug[id]) {
                        cnt++;
                        v = Vg[id];
                    }
                    else {
                        cnt--;
                        v = Ug[id];
                    }

                    ans.pb(id);
                    pos[id] = v;
                    last = id;
                    u = v;
                }
            }
            if(szant != 0) {
                for(int i = szant-1; i >= 0; i--) ans.pb(ans[i]);
            }
            break;
        }
    }

    if(ans.size()) {
        return ans;
    }
    return false;

}

Compilation message (stderr)

islands.cpp: In function 'std::variant<bool, std::vector<int, std::allocator<int> > > find_journey(int, int, std::vector<int>, std::vector<int>)':
islands.cpp:98:17: warning: unused variable 'cntt' [-Wunused-variable]
   98 |             int cntt = 0;
      |                 ^~~~
#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...