/**
* author: wxhtzdy
* created: 15.01.2023 19:07:48
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> par(n, -1);
vector<vector<int>> g(n);
vector<int> f(n);
function<int(int)> get = [&](int x) {
return par[x] < 0 ? x : par[x] = get(par[x]);
};
const int L = 18;
vector<int> dep(n);
vector<vector<int>> jump(n, vector<int>(L));
for (int i = 0; i < n; i++) {
jump[i] = vector<int>(L, i);
}
function<void(int, int)> Dfs = [&](int v, int pr) {
dep[v] = dep[pr] + 1;
jump[v][0] = pr;
for (int 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 (int i = L - 1; i >= 0; i--) {
if (dep[jump[v][i]] >= dep[u]) {
v = jump[v][i];
}
}
if (u == v) {
return u;
}
for (int i = L - 1; i >= 0; i--) {
if (jump[u][i] != jump[v][i]) {
u = jump[u][i];
v = jump[v][i];
}
}
return jump[u][0];
};
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
--u; --v;
assert(u != v);
if (get(u) != get(v)) {
int pu = get(u);
int 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 {
int w = LCA(u, v);
f[u] += 1;
f[v] += 1;
f[w] -= 2;
}
}
vector<pair<int, int>> ans;
vector<bool> was(n);
function<void(int, int)> Solve = [&](int v, int pr) {
was[v] = true;
for (int u : g[v]) {
if (u != pr) {
Solve(u, v);
f[v] += f[u];
}
}
if (f[v] == 0 && pr != -1) {
ans.emplace_back(v, pr);
}
};
for (int i = 0; i < n; i++) {
if (!was[i]) {
Solve(i, -1);
}
}
for (auto& p : ans) {
cout << p.first + 1 << " " << p.second + 1 << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
564 ms |
1292 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
544 ms |
1288 KB |
Output is correct |
2 |
Correct |
274 ms |
1248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3346 ms |
2416 KB |
Output is correct |
2 |
Correct |
919 ms |
2284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5047 ms |
4396 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5064 ms |
11512 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5064 ms |
13184 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5055 ms |
16076 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5009 ms |
16120 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5062 ms |
15268 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |