Submission #546561

# Submission time Handle Problem Language Result Execution time Memory
546561 2022-04-07T20:06:03 Z Soumya1 Pipes (CEOI15_pipes) C++17
100 / 100
1399 ms 14404 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef __LOCAL__
#include <debug_local.h>
#endif
const int mxN = 1e5 + 1;
const int lg = 18;
int s[mxN];
int par[mxN][lg];
int p[mxN];
vector<int> ad[mxN];
int d[mxN], sz[mxN];
void dfs(int u, int pp) {
  for (int j = 1; j < lg; j++) par[u][j] = par[par[u][j - 1]][j - 1];
  for (int v : ad[u]) {
    if (v == pp) continue;
    d[v] = d[u] + 1;
    par[v][0] = u;
    dfs(v, u);
  }
}
void unite_node(int u, int v) {
  if (sz[par[u][lg - 1]] > sz[par[v][lg - 1]]) swap(u, v);
  sz[par[v][lg - 1]] += sz[par[u][lg - 1]];
  par[u][0] = v;
  d[u] = d[v] + 1;
  ad[u].push_back(v);
  ad[v].push_back(u);
  dfs(u, v);
}
int find(int u) {
  return p[u] = (u == p[u] ? u : find(p[u]));
}
void unite_component(int u, int v) {
  u = find(u);
  v = find(v);
  if (u == v) return;
  if (s[u] > s[v]) swap(u, v);
  p[u] = v;
  s[v] += s[u];
}
int find_next(int u) {
  for (int j = lg - 1; j >= 0; j--) {
    if (find(par[u][j]) == find(u)) {
      u = par[u][j];
    }
  }
  if (find(u) == find(par[u][0])) return -1;
  return par[u][0];
}
int lca(int u, int v) {
  if (d[u] > d[v]) swap(u, v);
  for (int j = lg - 1; j >= 0; j--) {
    if (d[u] <= d[par[v][j]]) v = par[v][j];
  }
  if (u == v) return u;
  for (int j = lg - 1; j >= 0; j--) {
    if (par[u][j] != par[v][j]) u = par[u][j], v = par[v][j];
  }
  return par[u][0];
}
void unite_path(int u, int v) {
  int l = lca(u, v);
  vector<int> vv;
  while (u != -1 && d[u] >= d[l]) {
    vv.push_back(u);
    u = find_next(u);
  }
  vv.push_back(l);
  while (v != -1 && d[v] >= d[l]) {
    vv.push_back(v);
    v = find_next(v);
  }
  for (int x : vv) unite_component(x, l);
}
void testCase() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    p[i] = i;
    s[i] = 1;
    for (int j = 0; j < lg; j++) par[i][j] = i;
    sz[i] = 1;
  }
  while (m--) {
    int u, v;
    cin >> u >> v;
    if (par[u][lg - 1] != par[v][lg - 1]) {
      unite_node(u, v);
    } else if (find(u) != find(v)) {
      unite_path(u, v);
    }
  }
  set<pair<int, int>> st;
  for (int i = 1; i <= n; i++) {
    for (int j : ad[i]) {
      if (find(i) != find(j)) {
        st.insert({min(i, j), max(i, j)});
      }
    }
  }
  for (auto [a, b] : st) cout << a << " " << b << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  testCase();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 3156 KB Output is correct
2 Correct 8 ms 3156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 3028 KB Output is correct
2 Correct 85 ms 3076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 155 ms 3808 KB Output is correct
2 Correct 188 ms 3796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 251 ms 5432 KB Output is correct
2 Correct 233 ms 6120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 498 ms 10964 KB Output is correct
2 Correct 441 ms 10828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 732 ms 12112 KB Output is correct
2 Correct 590 ms 12052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 961 ms 14288 KB Output is correct
2 Correct 946 ms 14348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1267 ms 14176 KB Output is correct
2 Correct 1262 ms 14404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1399 ms 13724 KB Output is correct
2 Correct 1122 ms 14360 KB Output is correct