Submission #682106

# Submission time Handle Problem Language Result Execution time Memory
682106 2023-01-15T18:50:00 Z MilosMilutinovic Pipes (CEOI15_pipes) C++14
60 / 100
4667 ms 17236 KB
/**
 *    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 Correct 0 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 980 KB Output is correct
2 Correct 7 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 224 ms 900 KB Output is correct
2 Correct 214 ms 868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 490 ms 1928 KB Output is correct
2 Correct 493 ms 1896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 721 ms 4556 KB Output is correct
2 Correct 602 ms 5556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1293 ms 12052 KB Output is correct
2 Correct 1109 ms 12080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2080 ms 14028 KB Output is correct
2 Correct 1659 ms 13848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 3105 ms 17020 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3674 ms 17236 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4667 ms 16604 KB Memory limit exceeded
2 Halted 0 ms 0 KB -