Submission #100505

# Submission time Handle Problem Language Result Execution time Memory
100505 2019-03-11T19:54:21 Z doowey Pipes (CEOI15_pipes) C++14
100 / 100
1555 ms 13936 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);

const int N = (int)1e5 + 9;

struct DSU{
    int pi[N];
    int sz[N];
    int fin(int x){
        while(x!=pi[x]){
            x=pi[x];
        }
        return x;
    }
    bool unite(int a, int b){
        a = fin(a);
        b = fin(b);
        if(a==b)
            return false;
        if(sz[a] > sz[b])
            swap(a, b);
        sz[b] += sz[a];
        pi[a] = b;
        return true;
    }
    void Init(){
        for(int i = 0 ;i  < N ; i ++ ){
            pi[i] = i;
            sz[i] = 1;
        }
    }
};

DSU A, B;

vector<int> T[N];

int tin[N];
int low[N];
int timer;

bool vis[N];

void dfs(int node, int par){
    vis[node] = true;
    tin[node] = timer ++ ;
    low[node] = tin[node];
    for(auto x : T[node]){
        if(x == par){
            continue;
        }
        if(vis[x]){
            low[node] = min(low[node], tin[x]);
        }
        else{
            dfs(x, node);
            low[node] = min(low[node], low[x]);
            if(tin[node] < low[x] && B.fin(x) != B.fin(node)){
                cout << x << " " << node << "\n";
            }
        }
    }
}



int main(){
    fastIO;
    A.Init();
    B.Init();
    int n, m;
    cin >> n >> m;
    int u, v;
    int cnt = 0;
    for(int i = 0 ; i < m ; i ++ ){
        cin >> u >> v;
        if(A.unite(u,v)){
            T[u].push_back(v);
            T[v].push_back(u);
            cnt += 2 ;
        }
        else if(B.unite(u,v)){
            T[u].push_back(v);
            T[v].push_back(u);
            cnt += 2;
        }
    }
    assert(cnt <= (int)1e6);
    for(int i = 1; i <= n; i ++ )
        if(!vis[i])dfs(i,-1);
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4224 KB Output is correct
2 Correct 5 ms 4224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4736 KB Output is correct
2 Correct 8 ms 4480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 4576 KB Output is correct
2 Correct 114 ms 4436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 201 ms 5240 KB Output is correct
2 Correct 237 ms 4728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 338 ms 6536 KB Output is correct
2 Correct 293 ms 6180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 505 ms 11228 KB Output is correct
2 Correct 428 ms 8184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 799 ms 12036 KB Output is correct
2 Correct 770 ms 9596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1024 ms 13932 KB Output is correct
2 Correct 986 ms 9876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1279 ms 13936 KB Output is correct
2 Correct 1220 ms 9904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1555 ms 13436 KB Output is correct
2 Correct 1484 ms 10916 KB Output is correct