이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//
// Created by 42kangaroo on 30/08/2024.
//
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
constexpr int mod = 1e9 + 7;
using g_t = vector<vector<int>>;
int find(int a, vector<int>& p) {
return a == p[a] ? a : p[a] = find(p[a], p);
}
bool unite(int a, int b, vector<int>& p) {
a = find(a, p);
b = find(b, p);
if (a == b) return false;
p[a] = b;
return true;
}
ll dfs(int n, int p, g_t& g, vector<ll> noV, vector<bool>& vis) {
vis[n] = true;
ll re = noV[n];
for (auto e: g[n]) {
if (e == p) continue;
re ^= dfs(e, n, g, noV, vis);
}
if (re == 0 && n != p) cout << n + 1 << " " << p + 1 << "\n";
return re;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m; cin >> n >> m;
vector<int> p(n);
iota(p.begin(), p.end(), 0);
vector<ll> noV(n, 0);
g_t g(n);
mt19937 rng(0);
uniform_int_distribution<ll> dI(0, numeric_limits<ll>::max());
for (int i = 0; i < m; ++i) {
int a, b; cin >> a >> b;
--a; --b;
if (!unite(a, b, p)) {
ll nu = dI(rng);
noV[a] ^= nu;
noV[b] ^= nu;
} else {
g[a].push_back(b);
g[b].push_back(a);
}
}
vector<bool> vis(n, false);
for (int i = 0; i < n; ++i) {
if (!vis[i]) dfs(i, i, g, noV, vis);
}
}
# | 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... |
# | 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... |