답안 #437676

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
437676 2021-06-26T20:07:36 Z xiaowuc1 Pipes (CEOI15_pipes) C++17
100 / 100
1415 ms 13492 KB
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
// END NO SAD

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> matrix;

int find(vector<int>& p, int x) {
  return p[x] == x ? x : (p[x] = find(p, p[x]));
}
bool merge(vector<int>& p, int x, int y) {
  x = find(p, x);
  y = find(p, y);
  if(x == y) return false;
  p[x] = y;
  return true;
}

bool visited[100001];
vector<int> edges[100001];
int aptime = 1;
int discTime[100001];
int low[100001];

void dfsForAP(int curr, int parent) {
  visited[curr] = true;
  int numchild = 0;
  discTime[curr] = low[curr] = ++aptime;
  sort(all(edges[curr]));
  for(int i = 0; i < sz(edges[curr]); ) {
    int out = edges[curr][i];
    bool dup = i+1 < sz(edges[curr]) && edges[curr][i] == edges[curr][i+1];
    if(!visited[out]) {
      numchild++;
      dfsForAP(out, curr);
      low[curr] = min(low[curr], low[out]);
      if(!dup && low[out] > discTime[curr]) {
        cout << curr << " " << out << "\n";
      }
    }
    else if(out != parent) {
      low[curr] = min(low[curr], discTime[out]);
    }
    if(dup) i += 2;
    else i++;
  }
}

void solve() {
  int n, m;
  cin >> n >> m;
  {
    vector<int> p1(n+1);
    vector<int> p2(n+1);
    iota(all(p1), 0);
    iota(all(p2), 0);
    while(m--) {
      int a, b;
      cin >> a >> b;
      if(merge(p1, a, b)) {
        edges[a].pb(b);
        edges[b].pb(a);
      } 
      else if(merge(p2, a, b)) {
        edges[a].pb(b);
        edges[b].pb(a);
      }
    }
  }
  for(int i = 1; i <= n; i++) {
    if(visited[i]) continue;
    dfsForAP(i, -1);
  }
}

// what would chika do
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
// are you doing geometry in floating points

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3148 KB Output is correct
2 Correct 7 ms 2892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 2972 KB Output is correct
2 Correct 128 ms 2836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 214 ms 3776 KB Output is correct
2 Correct 267 ms 3240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 340 ms 5324 KB Output is correct
2 Correct 298 ms 5124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 497 ms 10228 KB Output is correct
2 Correct 424 ms 6820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 821 ms 11424 KB Output is correct
2 Correct 753 ms 8644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 998 ms 13488 KB Output is correct
2 Correct 949 ms 8464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1191 ms 13492 KB Output is correct
2 Correct 1114 ms 8520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1415 ms 12900 KB Output is correct
2 Correct 1405 ms 9928 KB Output is correct