Submission #35974

# Submission time Handle Problem Language Result Execution time Memory
35974 2017-12-04T03:53:52 Z funcsr Pipes (CEOI15_pipes) C++14
90 / 100
5000 ms 14068 KB
#include <iostream>
#include <fstream>
#include <bitset>
#include <cassert>
#include <vector>
#define rep(i, n) for (int i=0; i<(n); i++)
#define INF 1145141919
#define pb push_back

using namespace std;
struct UnionFind {
  int U[100000];
  UnionFind(int N) {
    rep(i, N) U[i] = i;
  }
  int find(int x) {
    if (U[x] == x) return x;
    return U[x] = find(U[x]);
  }
  void unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y) return;
    U[x] = y;
  }
  bool same(int x, int y) {
    return find(x) == find(y);
  }
};

int N, M;
vector<int> G[100000];

int R[100000];
int U[17][100000];
void dfs(int x, int p, int r) {
  R[x] = r;
  U[0][x] = p;
  for (int t : G[x]) {
    if (t == p) continue;
    dfs(t, x, r+1);
  }
}

inline int lca(int x, int y) {
  if (R[x] < R[y]) swap(x, y);
  for (int b=16; b>=0; b--) if ((R[x]-R[y])>>b) x = U[b][x];
  if (x == y) return x;
  for (int b=16; b>=0; b--) if (U[b][x] != U[b][y]) x = U[b][x], y = U[b][y];
  return U[0][x];
}

int T[100000];
int dfs2(int x, int p) {
  for (int t : G[x]) {
    if (t == p) continue;
    T[x] += dfs2(t, x);
  }
  if (p != -1 && T[x] == 0) cout << x+1 << " " << p+1 << "\n";
  int ret = T[x];
  T[x] = INF;
  return ret;
}


signed main() {
  ifstream cin("/dev/stdin");
  cin.tie(0); ios::sync_with_stdio(false);
  cin >> N >> M;
  auto uf = new UnionFind(N);
  auto backward = new bitset<6000000>();
  rep(i, M) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    if (uf->same(u, v)) {
      backward->set(i);
    }
    else {
      uf->unite(u, v);
      G[u].pb(v);
      G[v].pb(u);
    }
  }
  delete uf;
  rep(i, N) R[i] = -1;
  rep(i, N) if (R[i] == -1) dfs(i, -1, 0);
  rep(k, 16) rep(i, N) {
    if (U[k][i] == -1) U[k+1][i] = -1;
    U[k+1][i] = U[k][U[k][i]];
  }

  cin.seekg(0);
  int nn, nm;
  cin >> nn >> nm;
  assert(N == nn && M == nm);
  rep(i, M) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    if (backward->test(i)) {
      T[u]++;
      T[v]++;
      T[lca(u, v)]-=2;
    }
  }
  delete backward;
  rep(i, N) if (T[i] != INF) dfs2(i, -1);
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3456 KB Output is correct
2 Correct 5 ms 3580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 3948 KB Output is correct
2 Correct 10 ms 4076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 262 ms 3816 KB Output is correct
2 Correct 252 ms 3944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 521 ms 4560 KB Output is correct
2 Correct 594 ms 4432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 917 ms 6020 KB Output is correct
2 Correct 755 ms 6660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1297 ms 10940 KB Output is correct
2 Correct 1131 ms 10856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2150 ms 12040 KB Output is correct
2 Correct 1901 ms 11976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3094 ms 13908 KB Output is correct
2 Correct 3025 ms 14060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4167 ms 14036 KB Output is correct
2 Correct 4051 ms 14068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5032 ms 13376 KB Time limit exceeded
2 Halted 0 ms 0 KB -