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 <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1, M = 6e6, LOG = 17;
int h[N], val[N], par[N][LOG];
bitset <M> E;
vector <int> g[N];
int getpar(int v, int height) {
for (int i = 0; height; height>>=1, i++) if (height&1) v = par[v][i];
return v;
}
int LCA(int u, int v) {
if (h[u] > h[v]) swap(u, v);
v = getpar(v, h[v] - h[u]);
if (u == v) return v;
for (int i = LOG - 1; i >= 0; i--) if (par[v][i] != par[u][i]) v = par[v][i], u = par[u][i];
return par[v][0];
}
void init(int n) {for (int i = 1; i <= n; i++) par[i][0] = i; return ;}
int getroot(int u) {return par[u][0] = (par[u][0] == u ? u : getroot(par[u][0]));}
void Union(int u, int v, int id) {
int ru = getroot(u), rv = getroot(v);
if (ru == rv) {E[id] = 0; return ;}
par[ru][0] = rv, g[u].push_back(v), g[v].push_back(u);
}
void dfs(int v) {
for (int i = 1; i < LOG; i++) par[v][i] = par[par[v][i - 1]][i - 1];
for (int u : g[v]) if (!h[u]) h[u] = h[v] + 1, par[u][0] = v, dfs(u);
}
void Solve(int v) {
for (int u : g[v]) if (!h[u]) h[u] = 1, Solve(u), val[v] += val[u];
if (par[v][0] && val[v]) cout << v << " " << par[v][0] << "\n";
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m; cin >> n >> m, init(n), E.set();
for (int i = 0; i < m; i++) {int u, v; cin >> u >> v, Union(u, v, i);}
for (int i = 1; i <= n; i++) if (!h[i]) par[i][0] = i, h[i] = 1, dfs(i);
memset(h, 0, sizeof h), cin.seekg(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
if (!E[i]) val[u]++, val[v]++, val[LCA(u, v)] -= 2;
}
for (int i = 1; i <= n; i++) if (!h[i]) Solve(i);
}
# | 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... |