CodeForces-732F.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <queue>
  4. #include <utility>
  5. #include <vector>
  6. using namespace std;
  7. struct E { int to, id; };
  8. int main() {
  9. ios::sync_with_stdio(false);
  10. cin.tie(nullptr);
  11. int n, m; cin >> n >> m;
  12. vector<pair<int, int>> edge(m), answer(m, {-1, -1});
  13. vector<vector<E>> g(n);
  14. for (int id = 0; id < m; ++id) {
  15. int u, v; cin >> u >> v; --u; --v; edge[id] = {u, v};
  16. g[u].push_back({v, id}); g[v].push_back({u, id});
  17. }
  18. vector<int> dfn(n), low(n), par(n, -1), pe(n, -1);
  19. vector<size_t> it(n); vector<bool> bridge(m); int timer = 0;
  20. vector<int> st{0}; dfn[0] = low[0] = ++timer;
  21. while (!st.empty()) {
  22. int u = st.back();
  23. if (it[u] < g[u].size()) {
  24. E e = g[u][it[u]++];
  25. if (e.id == pe[u]) continue;
  26. if (!dfn[e.to]) {
  27. par[e.to] = u; pe[e.to] = e.id;
  28. dfn[e.to] = low[e.to] = ++timer; st.push_back(e.to);
  29. } else low[u] = min(low[u], dfn[e.to]);
  30. } else {
  31. st.pop_back();
  32. if (par[u] != -1) {
  33. if (low[u] > dfn[par[u]]) bridge[pe[u]] = true;
  34. low[par[u]] = min(low[par[u]], low[u]);
  35. }
  36. }
  37. }
  38. vector<int> comp(n, -1), comp_size; int count = 0;
  39. for (int s = 0; s < n; ++s) if (comp[s] == -1) {
  40. int size = 0; vector<int> work{s}; comp[s] = count;
  41. while (!work.empty()) {
  42. int u = work.back(); work.pop_back(); ++size;
  43. for (E e : g[u]) if (!bridge[e.id] && comp[e.to] == -1) {
  44. comp[e.to] = count; work.push_back(e.to);
  45. }
  46. }
  47. comp_size.push_back(size); ++count;
  48. }
  49. int root = int(max_element(comp_size.begin(), comp_size.end()) - comp_size.begin());
  50. // 非桥边:在各边双分量中做 DFS,树边向下、返祖边向上。
  51. vector<bool> seen_vertex(n), used_edge(m);
  52. vector<size_t> orient_it(n);
  53. for (int s = 0; s < n; ++s) if (!seen_vertex[s]) {
  54. vector<int> dfs{s}; seen_vertex[s] = true;
  55. while (!dfs.empty()) {
  56. int u = dfs.back();
  57. while (orient_it[u] < g[u].size() &&
  58. (bridge[g[u][orient_it[u]].id] || used_edge[g[u][orient_it[u]].id])) {
  59. ++orient_it[u];
  60. }
  61. if (orient_it[u] == g[u].size()) { dfs.pop_back(); continue; }
  62. E e = g[u][orient_it[u]++];
  63. used_edge[e.id] = true; answer[e.id] = {u, e.to};
  64. if (!seen_vertex[e.to]) { seen_vertex[e.to] = true; dfs.push_back(e.to); }
  65. }
  66. }
  67. vector<vector<pair<int, int>>> tree(count);
  68. for (int id = 0; id < m; ++id) if (bridge[id]) {
  69. auto [u, v] = edge[id];
  70. tree[comp[u]].push_back({comp[v], id});
  71. tree[comp[v]].push_back({comp[u], id});
  72. }
  73. vector<int> parent(count, -1); queue<int> q; parent[root] = root; q.push(root);
  74. while (!q.empty()) {
  75. int c = q.front(); q.pop();
  76. for (auto [to, id] : tree[c]) if (parent[to] == -1) {
  77. parent[to] = c; q.push(to);
  78. auto [u, v] = edge[id];
  79. // 子分量 -> 父分量
  80. answer[id] = (comp[u] == to) ? pair<int, int>{u, v}
  81. : pair<int, int>{v, u};
  82. }
  83. }
  84. cout << comp_size[root] << '\n';
  85. for (auto [u, v] : answer) cout << u + 1 << ' ' << v + 1 << '\n';
  86. }