Submission #1081297

# Submission time Handle Problem Language Result Execution time Memory
1081297 2024-08-29T21:17:51 Z jk_ Pipes (CEOI15_pipes) C++14
100 / 100
987 ms 12116 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 100000;

struct ufind {
  int *c;
  ufind(int *c) : c(c) { iota(c, c+N, 0); }
  int find(int k) {
    if (c[k] == k) return k;
    while (c[k] != c[c[k]])
      c[k] = c[c[k]];
    return c[k];
  }
  void unite(int a, int b) {
    c[find(a)] = find(b);
  }
};

struct edge { int to; int next; };
edge edges[4*N+4];
int edges_i = 0;
int g[N];

int timer=0;
int pre[N];
int low[N];
bitset<2*N> evis;

void dfs(int v) {
  pre[v] = low[v] = timer++;
  for (;;) {
    int e = g[v];
    if (e == -1) return;
    g[v] = edges[e].next;
    int w = edges[e].to;
    if (evis[e/2]) { continue; }
    evis[e/2] = true;
    if (pre[w] != -1) {
      low[v] = min(low[v], pre[w]);
    } else {
      dfs(w);
      low[v] = min(low[v], low[w]);
      if (low[w] > pre[v]) {
        cout << v+1 << ' ' << w+1 << '\n';
      }
    }
  }
};

int main() {
  fill(g, g+N, -1);
  fill(edges, edges+4*N, edge{-1,-1});

  int n, m; scanf("%d%d", &n, &m);
  assert(n <= N);
  {
    array<ufind, 2> msts{{ufind(pre), ufind(low)}};

    for (int i = 0; i < m; ++i) {
      int a, b; scanf("%d%d", &a, &b); --a; --b;
      for (int j = 0; j<2; ++j) {
        if (msts[j].find(a) != msts[j].find(b)) {
          msts[j].unite(a, b);

          edges[edges_i] = {b, g[a]};
          g[a] = edges_i++;

          edges[edges_i] = {a, g[b]};
          g[b] = edges_i++;

          assert(edges_i <= 4*n);

          break;
        }
      }
    }
  }

  fill(pre, pre+N, -1);
  fill(low, low+N, -1);

  for (int i = 0; i < n; ++i)
    if (pre[i] == -1)
      dfs(i);
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:54:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |   int n, m; scanf("%d%d", &n, &m);
      |             ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:60:22: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |       int a, b; scanf("%d%d", &a, &b); --a; --b;
      |                 ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4700 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4952 KB Output is correct
2 Correct 4 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 4740 KB Output is correct
2 Correct 64 ms 4736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 113 ms 5200 KB Output is correct
2 Correct 125 ms 4724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 223 ms 6480 KB Output is correct
2 Correct 168 ms 5744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 375 ms 9856 KB Output is correct
2 Correct 260 ms 5456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 534 ms 10576 KB Output is correct
2 Correct 441 ms 7760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 709 ms 12116 KB Output is correct
2 Correct 608 ms 5744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 846 ms 12112 KB Output is correct
2 Correct 703 ms 5748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 987 ms 11856 KB Output is correct
2 Correct 821 ms 8528 KB Output is correct