답안 #95121

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
95121 2019-01-27T12:51:35 Z MrTEK Pipes (CEOI15_pipes) C++14
100 / 100
1238 ms 14604 KB
/**
  Normalde geciyor oj.uz'un malligi oyuzden geciyorum.
*/
 
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
 
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
 
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
 
const int MOD = 1000000007;
template<typename T>
ostream& operator<< (ostream& out, const pair<T, T>& v) {
    out << "{" << v.first << ", " << v.second << "}";
    return out;
}
 
template<typename T>
ostream& operator<< (ostream& out, const vector<T>& v) {
    out << "[";
    size_t last = v.size() - 1;
    for(size_t i = 0; i < v.size(); ++i) {
        out << v[i];
        if (i != last) 
            out << ", ";
    }
    out << "]";
    return out;
}
 
template<typename T>
ostream& operator<< (ostream& out, const set<T>& v) {
    out << "[";
    auto pen = v.end();
    pen--;
    for (auto it = v.begin(); it != v.end(); it++){
    out << *it;
    if (it != pen){
      out << ", ";
    }
  }
  out << "]";
    return out;
}
 
template<typename T>
ostream& operator<< (ostream& out, const Tree<T>& v) {
    out << "[";
    auto pen = v.end();
    pen--;
    for (auto it = v.begin(); it != v.end(); it++){
    out << *it;
    if (it != pen){
      out << ", ";
    }
  }
  out << "]";
    return out;
}
 
template<int SZ> struct DSU {
    int par[SZ];
    DSU() {
        F0R(i,SZ) par[i] = i;
    }
    
    int get(int x) { // path compression
      if (par[x] != x) par[x] = get(par[x]);
      return par[x];
    }
    
    bool unite(int x, int y) { // union-by-rank
      x = get(x), y = get(y);
      if (x == y) return 0;
      par[y] = x;
      return 1;
    }
};
 
DSU<100005> one_connected;
DSU<100005> two_connected; 
vector<int> adj[100005];
vector<int> adj2[100005];
map<pii, int> cnt;
int mxlabel[100005];
int mnlabel[100005];
int label[100005];
int par1[100005];
int par2[100005];
int sz[100005];
bool vis[100005];
int timer = 1;
 
void DFS(int v, int p = -1){
  //cout << v << ' ' << p << endl;
  vis[v] = true;
  label[v] = timer++;
  sz[v] = 1;
  for (int j: adj[v]){
    if (j == p) continue;
    DFS(j, v);
    sz[v] += sz[j];
  }
}
 
void DFS2(int v, int p = -1){
  vis[v] = true;
  mxlabel[v] = -MOD;
  mnlabel[v] = MOD;
  for (int j: adj2[v]){
    if (j != p){
      mxlabel[v] = max(mxlabel[v], label[j]);
      mnlabel[v] = min(mnlabel[v], label[j]);
    }
  }
  for (int j: adj[v]){
    if (j != p){
      DFS2(j, v);
    }
  }
}
 
void DFS3(int v, int p = -1){
  vis[v] = true;
  for (int j: adj[v]){
    if (j != p){
      DFS3(j, v);
      mxlabel[v] = max(mxlabel[v], mxlabel[j]);
      mnlabel[v] = min(mnlabel[v], mnlabel[j]);
    }
  }
}
 
void DFS4(int v, int p = -1){
  vis[v] = true;
  for (int j: adj[v]){
    if (j != p){
      DFS4(j, v);
      bool is_bridge = true;
      if (mnlabel[j] < label[j]){
        is_bridge = false;
      }
      if (mxlabel[j] >= label[j] + sz[j]){
        is_bridge = false;
      }
      if (is_bridge){
        int cnt = 0;
        if ((par1[v] == j) || (par1[j] == v)) cnt++;
        if ((par2[v] == j) || (par2[j] == v)) cnt++;
        if (cnt == 1) cout << v << ' ' << j << endl;
      }
    }
  }
}
 
void DFSParent1(int v, int p = -1){
  vis[v] = true;
  for (int j: adj[v]){
    if (j != p){
      DFSParent1(j, v);
      par1[j] = v;
    }
  }
}
 
void DFSParent2(int v, int p = -1){
  vis[v] = true;
  for (int j: adj2[v]){
    if (j != p){
      DFSParent2(j, v);
      par2[j] = v;
    }
  }
}
 
 
int main() {
  ios_base::sync_with_stdio(0);cin.tie(0);
  int n, m; cin >> n >> m;
  int u, v;
  for (int i = 0; i < m; i++){
    cin >> u >> v;
    if (!one_connected.unite(u, v)){
      if (!two_connected.unite(u, v)){
        continue;
      }
      else{
        adj2[u].pb(v);
        adj2[v].pb(u);
      }
    }
    else{
      adj[u].pb(v);
      adj[v].pb(u);
    }
  }
  for (int i = 1; i <= n; i++){
    if (!vis[i]){
      DFSParent1(i);
    }
  }
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; i++){
    if (!vis[i]){
      DFSParent2(i);
    }
  }
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; i++){
    if (!vis[i]){
      DFS(i);
    }
  }
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; i++){
    if (!vis[i]){
      DFS2(i);
    }
  }
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; i++){
    if (!vis[i]){
      DFS3(i);
    }
  }
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; i++){
    if (!vis[i]){
      DFS4(i);
    }
  }
  
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5988 KB Output is correct
2 Correct 7 ms 6016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 6272 KB Output is correct
2 Correct 11 ms 6272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 6216 KB Output is correct
2 Correct 96 ms 6236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 167 ms 6724 KB Output is correct
2 Correct 187 ms 6712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 289 ms 8000 KB Output is correct
2 Correct 257 ms 8004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 515 ms 11940 KB Output is correct
2 Correct 394 ms 12016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 715 ms 12944 KB Output is correct
2 Correct 656 ms 11512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 962 ms 14556 KB Output is correct
2 Correct 951 ms 14604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1067 ms 14556 KB Output is correct
2 Correct 1032 ms 14584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1238 ms 14072 KB Output is correct
2 Correct 1226 ms 13176 KB Output is correct