Submission #100632

# Submission time Handle Problem Language Result Execution time Memory
100632 2019-03-12T23:34:05 Z doowey Pipes (CEOI15_pipes) C++14
100 / 100
1341 ms 8668 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

mt19937 rnd(chrono::steady_clock().now().time_since_epoch().count());

const int N = (int)1e5+1;
int n1[N], n2[N];
int tin[N], low[N];
int nd[4 * N], pv[4 * N];
int las[N];
int timer;
int cnt;

void add(int a, int b){ // a -> b
    nd[cnt]=b;
    pv[cnt]=las[a];
    las[a]=cnt;
    cnt ++ ;
}

void EDGE(int a, int b){
    add(a,b);
    add(b,a);
}

vector<int> vis;
int fin1(int x){
    vis.clear();
    vis.push_back(x);
    while(x!=n1[x]){
        x=n1[x];
        vis.push_back(x);
    }
    for(auto p : vis) n1[p]=x;
    return x;
}
int fin2(int x){
    vis.clear();
    vis.push_back(x);
    while(x!=n2[x]){
        x=n2[x];
        vis.push_back(x);
    }
    for(auto p : vis) n2[p]=x;
    return x;
}

bool merg1(int a, int b){
    a=fin1(a);
    b=fin1(b);
    if(a==b) return false;
    if((int)rnd()%2)n1[a]=b;
    else n1[b] = a;
    return true;
}
bool merg2(int a, int b){
    a=fin2(a);
    b=fin2(b);
    if(a==b) return false;
    if((int)rnd()%2)n2[a]=b;
    else n2[b]=a;
    return true;
}

void Init(){
    for(int i = 0 ; i < N ; i ++ ){
        n1[i] = i, n2[i] = i;
        tin[i] = -1, low[i] = -1;
        las[i] = -1;
    }
    for(int i = 0 ; i < 4 * N ; i ++ ){
        nd[i] = -1;
        pv[i] = -1;
    }
}

void dfs(int node, int par){
    tin[node] = timer ++ ;
    low[node] = tin[node];
    for(int j = las[node] ; j != -1 ; j = pv[j]){
        if(nd[j] == par){
            continue;
        }
        if(tin[nd[j]] != -1){
            low[node] = min(low[node], tin[nd[j]]);
        }
        else{
            dfs(nd[j], node);
            low[node] = min(low[node], low[nd[j]]);
            if(tin[node] < low[nd[j]] && fin2(node) != fin2(nd[j])){
                cout << node << " " << nd[j] << "\n";
            }
        }
    }
}

int main(){
    fastIO;
    Init();
    int n, m;
    cin >> n >> m;
    int u, v;
    for(int i = 0 ; i < m ; i ++ ){
        cin >> u >> v;
        if(merg1(u,v))
            EDGE(u, v);
        else if(merg2(u,v))
            EDGE(u, v);
    }
    for(int i =1 ; i <= n; i ++ )
        if(tin[i] == -1) dfs(i,-1);
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 6 ms 5376 KB Output is correct
2 Correct 6 ms 5376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 5504 KB Output is correct
2 Correct 8 ms 5376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 5496 KB Output is correct
2 Correct 108 ms 5376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 185 ms 5732 KB Output is correct
2 Correct 221 ms 5496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 318 ms 6108 KB Output is correct
2 Correct 279 ms 5884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 437 ms 7672 KB Output is correct
2 Correct 405 ms 5752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 725 ms 8056 KB Output is correct
2 Correct 714 ms 6752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 881 ms 8668 KB Output is correct
2 Correct 846 ms 5852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1094 ms 8668 KB Output is correct
2 Correct 1062 ms 5880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1341 ms 8420 KB Output is correct
2 Correct 1314 ms 7004 KB Output is correct