# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1242778 | Zbyszek99 | Thousands Islands (IOI22_islands) | C++20 | 0 ms | 0 KiB |
#include "islands.h"
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace std;
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
vector<pii> graph_pom[100001];
vector<pii> graph[100001];
vector<pii> graph_trans[100001];
vi comp_graph[100001];
bool was_cycle[100001];
bool odw[100001];
vi post;
int cur_comp = 0;
int comp[100001];
vi comp_list[100001];
void dfs(int v)
{
odw[v] = 1;
forall(it,graph[v])
{
if(odw[it.ff] == 0) dfs(it.ff);
}
post.pb(v);
}
void dfs_comp(int v)
{
comp_list[cur_comp].pb(v);
comp[v] = cur_comp;
odw[v] = 1;
forall(it,graph_trans[v])
{
if(odw[it.ff] == 0) dfs_comp(it.ff);
}
}
variant<bool,vi> find_journey(int n, int m, vi U, vi V)
//vi find_journey(int n, int m, vi u, vi v)
{
unordered_set<int> used;
rep(i,m)
{
graph_pom[u[i]].pb({v[i],i});
}
int cur_v = 0;
vi ans;
while(siz(graph_pom[cur_v]) <= 1)
{
if(siz(graph_pom[cur_v]) == 0) return 0;
if(odw[cur_v] == 1) return 0;
odw[cur_v] = 1;
used.insert(graph_pom[cur_v][0].ss);
ans.pb(graph_pom[cur_v][0].ss);
cur_v = graph_pom[cur_v][0].ff;
}
rep(i,m)
{
if(used.find(i) == used.end())
{
graph[u[i]].pb({v[i],i});
graph_trans[v[i]].pb({u[i],i});
}
}
rep(i,n) odw[i] = 0;
rep(i,n)
{
if(odw[i] == 0)
{
dfs(i);
}
}
reverse(all(post));
rep(i,n) odw[i] = 0;
forall(it,post)
{
if(odw[it] == 0)
{
dfs_comp(it);
cur_comp++;
}
}
rep(i,cur_comp)
{
forall(v,comp_list[i])
{
forall(it,graph[v])
{
if(comp[it.ff] != comp[v])
{
comp_graph[comp[v]].pb(comp[it.ff]);
}
}
}
}
for(int c = cur_comp-1; c >= 0; c--)
{
if(siz(comp_list[c]) > 1) was_cycle[c] = 1;
forall(it,comp_graph[c])
{
was_cycle[c] = was_cycle[c] || was_cycle[it];
}
}
vi good_edges;
forall(it,graph[cur_v])
{
if(was_cycle[comp[it.ff]])
{
good_edges.pb(it.ss);
}
}
if(siz(good_edges) <= 1) return 0;
return ans;
}