답안 #967632

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
967632 2024-04-22T14:29:20 Z steveonalex Pipes (CEOI15_pipes) C++17
100 / 100
801 ms 7764 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
 
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
 
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b){a = b; return true;}
        return false;
    }
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b){a = b; return true;}
        return false;
    }
template <class T>
    void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){
        for(auto i: a) out << i << separator;
        out << finish;
    }
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }


const int N = 1e5 + 69;

struct DSU{
    int n;
    int parent[N];

    DSU(){
        n = N-1;
        for(int i = 0; i<=n; ++i) 
            parent[i] = i;
    }

    int find_set(int u){return (u == parent[u]) ? u : (parent[u] = find_set(parent[u]));}
    bool same_set(int u,int v){return find_set(u) == find_set(v);}

    bool join_set(int u, int v){
        u = find_set(u), v = find_set(v);
        if (u != v){
            parent[v] = u;
            return true;
        }
        return false;
    }
};

DSU mst1, mst2;

vector<int> graph[N];
int last[N];
int dfs_cnt = 0;
int edge_cnt = 0;

void tarjan(int u, int p){
    #define num mst1.parent
    #define low mst2.parent
    num[u] = low[u] = ++dfs_cnt;
    for(int v: graph[u]){
        if (v == p){p = -1; continue;}
        if (num[v]) minimize(low[u], num[v]);
        else{
            tarjan(v, u);
            minimize(low[u], low[v]);
            if (low[v] >= num[v]){
                cout << u << " " << v << "\n";
            }
        }
    }
}

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n, m; cin >> n >> m;
    for(int i = 1; i<=m; ++i){
        int u, v; cin >> u >> v;
        if (mst1.join_set(u, v)){
            edge_cnt++;
            graph[u].push_back(v);
            graph[v].push_back(u);
        }
        else{
            mst2.join_set(u, v);
        }
    }

    for(int i= 1; i<=n; ++i) last[mst2.find_set(i)] = i;
    for(int i = 1; i<=n; ++i){
        int u = last[mst2.find_set(i)], v = i;
        if (u != v){
            graph[u].push_back(v);
            graph[v].push_back(u);
        }
        else last[mst2.find_set(i)] = i;
    }

    for(int i = 1; i<=n; ++i) mst1.parent[i] = mst2.parent[i] = 0;

    for(int i = 1; i<=n; ++i) if (num[i] == 0) tarjan(i, i);

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3676 KB Output is correct
2 Correct 1 ms 3676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3932 KB Output is correct
2 Correct 3 ms 3940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 3908 KB Output is correct
2 Correct 65 ms 3908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 4092 KB Output is correct
2 Correct 128 ms 4088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 193 ms 4540 KB Output is correct
2 Correct 184 ms 4952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 268 ms 6608 KB Output is correct
2 Correct 240 ms 6480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 404 ms 6968 KB Output is correct
2 Correct 422 ms 6804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 499 ms 7560 KB Output is correct
2 Correct 513 ms 7764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 659 ms 7632 KB Output is correct
2 Correct 604 ms 7504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 773 ms 7376 KB Output is correct
2 Correct 801 ms 7756 KB Output is correct