This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "islands.h"
#include <bits/stdc++.h>
using namespace std;
struct DSU{
vector<int> par;
DSU(int n){
par.resize(n);
iota(par.begin(), par.end(), 0);
}
int find(int node){
if (par[node]==node) return node;
return par[node]=find(par[node]);
}
void merge(int a, int b){
a=find(a);
b=find(b);
par[a]=b;
}
};
std::variant<bool, std::vector<int>> find_journey(
int N, int M, std::vector<int> U, std::vector<int> V) {
vector<vector<pair<int,int>>> arr(N);
for (int i = 0; i < M; i+=2){
arr[U[i]].push_back({V[i],i});
}
vector<bool> vis(N,false);
vector<pair<int,int>> par(N,{-1,-1});
vector<int> dept(N,0);
function<void(int)> dfs = [&](int node)->void{
if (vis[node]) return;
vis[node]=true;
for (auto it : arr[node]){
if (vis[it.first]) continue;
dept[it.first]=dept[node]+1;
par[it.first]={node,it.second};
dfs(it.first);
}
};
dfs(0);
DSU dsu(N);
vector<pair<int,int>> gidio(N,{-1,-1});
for (int i = 0; i < N; i++){
if (!vis[i]) continue;
if (dsu.find(i)!=i) continue;
for (auto it : arr[i]){
if (!vis[it.first]) continue;
if (dsu.find(i)==dsu.find(it.first)){
gidio[i]=it;
vector<int> aynilar;
for (int j = 0; j < N; j++){
if (dsu.find(i)==dsu.find(j)) aynilar.push_back(j);
}
sort(aynilar.begin(), aynilar.end(), [&](int a, int b){
return dept[a]<dept[b];
});
vector<int> ans;
int nd = aynilar[0];
while (nd!=-1){
if (par[nd].first!=-1) ans.push_back(par[nd].second);
nd=par[nd].first;
}
reverse(ans.begin(), ans.end());
ans.push_back(gidio[aynilar[0]].second);
nd = gidio[aynilar[0]].first;
while (nd!=i){
ans.push_back(gidio[nd].second);
nd=gidio[nd].first;
}
nd = aynilar[0];
while (nd!=i){
ans.push_back(gidio[nd].second+1);
nd=gidio[nd].first;
}
while (nd!=-1){
if (par[nd].first!=-1) ans.push_back(par[nd].second);
nd=par[nd].first;
}
return ans;
}
else {
dsu.merge(i,it.first);
gidio[i]=it;
break;
}
}
}
return false;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |