/**
* author: wxhtzdy
* created: 15.01.2023 19:07:48
**/
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100000;
const int L = 16;
int n, m;
vector<int> g[MAX];
int f[MAX];
int i;
int j;
int par[MAX];
int dep[MAX];
int jump[MAX][L];
int u;
int v;
int pu;
int pv;
int w;
void SolveFirstSubtask() {
for (i = 0; i < m; i++) {
cin >> u >> v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
function<void(int, int)> Dfs = [&](int v, int pr) {
dep[v] = dep[pr] + 1;
f[v] = dep[v];
for (int u : g[v]) {
if (u == pr) {
continue;
}
if (!dep[u]) {
Dfs(u, v);
f[v] = min(f[v], f[u]);
if (f[u] > dep[v]) {
cout << v + 1 << " " << u + 1 << '\n';
}
} else {
f[v] = min(f[v], dep[u]);
}
}
};
for (int i = 0; i < n; i++) {
if (!dep[i]) {
Dfs(i, i);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
if (n <= 100 && m <= 200) {
SolveFirstSubtask();
return 0;
}
assert(false);
for (i = 0; i < n; i++) {
par[i] = -1;
}
function<int(int)> get = [&](int x) {
return par[x] < 0 ? x : par[x] = get(par[x]);
};
for (i = 0; i < n; i++) {
for (j = 0; j < L; j++) {
jump[i][j] = i;
}
}
function<void(int, int)> Dfs = [&](int v, int pr) {
dep[v] = dep[pr] + 1;
jump[v][0] = pr;
for (j = 1; j < L; j++) {
jump[v][j] = jump[jump[v][j - 1]][j - 1];
}
for (int u : g[v]) {
if (u != pr) {
Dfs(u, v);
}
}
};
auto LCA = [&](int u, int v) {
if (dep[u] > dep[v]) {
swap(u, v);
}
for (j = L - 1; j >= 0; j--) {
if (dep[jump[v][j]] >= dep[u]) {
v = jump[v][j];
}
}
if (u == v) {
return u;
}
for (j = L - 1; j >= 0; j--) {
if (jump[u][j] != jump[v][j]) {
u = jump[u][j];
v = jump[v][j];
}
}
return jump[u][0];
};
for (i = 0; i < m; i++) {
cin >> u >> v;
--u; --v;
if (get(u) != get(v)) {
pu = get(u);
pv = get(v);
if (par[pu] > par[pv]) {
swap(pu, pv);
swap(u, v);
}
Dfs(v, u);
g[u].push_back(v);
g[v].push_back(u);
par[pu] += par[pv];
par[pv] = pu;
} else {
w = LCA(u, v);
f[u] += 1;
f[v] += 1;
f[w] -= 2;
}
}
function<void(int, int)> Solve = [&](int v, int pr) {
for (int u : g[v]) {
if (u != pr) {
Solve(u, v);
f[v] += f[u];
}
}
if (f[v] == 0 && pr != -1) {
cout << v + 1 << " " << pr + 1 << '\n';
}
};
for (i = 0; i < n; i++) {
if (get(i) == i) {
Solve(i, -1);
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4 ms |
5204 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4 ms |
5204 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
5204 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4 ms |
5204 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
5 ms |
5204 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4 ms |
5204 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
5 ms |
5252 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4 ms |
5204 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4 ms |
5204 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |