Submission #100620

# Submission time Handle Problem Language Result Execution time Memory
100620 2019-03-12T21:18:27 Z doowey Pipes (CEOI15_pipes) C++14
100 / 100
1275 ms 12632 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+1;
int n1[N];
int n2[N];

int fin1(int x){
    if(n1[x]==x)
        return x;
    return n1[x]=fin1(n1[x]);
}

int fin2(int x){
    if(n2[x]==x)
        return x;
    return n2[x]=fin2(n2[x]);
}

bool merge1(int a, int b){
    a=fin1(a);
    b=fin1(b);
    if(a==b) return false;
    n1[a]=b;
    return true;
}

bool merge2(int a,int b){
    a=fin2(a);
    b=fin2(b);
    if(a==b) return false;
    n2[a]=b;
    return true;
}

vector<int> T[N];
int tin[N];
int low[N];
bool vis[N];
int timer;


void dfs(int u, int par){
    tin[u] = timer ++ ;
    low[u] = tin[u];
    vis[u] = true;
    int x;
    while(!T[u].empty()){
        x = T[u].back();
        T[u].pop_back();
        if(x==par) continue;
        if(vis[x]){
            low[u] = min(low[u], tin[x]);
        }
        else{
            dfs(x,u);
            low[u] = min(low[u], low[x]);
            if(tin[u]<low[x] && fin2(u) != fin2(x)){
                cout << u << " " << x << "\n";
            }
        }
    }
}

int main(){
    fastIO;
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; i ++ ){
        n1[i]=i;
        n2[i]=i;
    }
    int u, v;
    for(int i = 0 ; i < m ; i ++ ){
        cin >> u >> v;
        if(merge1(u,v)){
            T[u].push_back(v);
            T[v].push_back(u);
        }
        else if(merge2(u,v)){
            T[u].push_back(v);
            T[v].push_back(u);
        }

    }
    for(int i = 1; i <= n; i ++ )if(!vis[i]) dfs(i,-1);
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2688 KB Output is correct
2 Correct 3 ms 2660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 3200 KB Output is correct
2 Correct 7 ms 2944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 3052 KB Output is correct
2 Correct 97 ms 2916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 163 ms 3596 KB Output is correct
2 Correct 198 ms 3324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 302 ms 5036 KB Output is correct
2 Correct 278 ms 4828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 464 ms 9692 KB Output is correct
2 Correct 424 ms 7160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 705 ms 10716 KB Output is correct
2 Correct 639 ms 8504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 868 ms 12632 KB Output is correct
2 Correct 817 ms 8960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1099 ms 12616 KB Output is correct
2 Correct 1046 ms 8952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1275 ms 12132 KB Output is correct
2 Correct 1180 ms 9940 KB Output is correct