| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1368006 | mannshah1211 | Thousands Islands (IOI22_islands) | C++20 | 0 ms | 0 KiB |
#include "islands.h"
#include <bits/stdc++.h>
#include <variant>
#include <vector>
using namespace std;
class dsu {
public:
vector<int> p;
int n;
dsu(int _n) : n(_n) {
p.resize(n);
iota(p.begin(), p.end(), 0);
}
int get(int x) {
if (p[x] == x) {
return x;
}
return (p[x] = get(p[x]));
}
bool unite(int x, int y) {
x = get(x);
y = get(y);
if (x != y) {
p[y] = x;
return true;
}
return false;
}
};
variant<bool, vector<int>> find_journey(int n, int m, vector<int> u, vector<int> v) {
vector<vector<int>> g(n);
for (int i = 0; i < m; i += 2) {
g[u[i]].push_back(v[i]);
g[v[i]].push_back(u[i]);
}
vector<bool> vis(n);
auto Dfs = [&](auto&& self, int v) -> void {
vis[v] = true;
for (int u : g[v]) {
if (!vis[u]) {
self(self, u);
}
}
};
Dfs(Dfs, 0);
bool cyc = false;
for (int i = 0; i < m; i += 2) {
if (vis[u[i]] && vis[v[i]]) {
if (!ds.unite(u[i], v[i])) {
cyc = true;
}
}
}
if (cyc) {
return true;
}
return false;
}