Submission #669607

# Submission time Handle Problem Language Result Execution time Memory
669607 2022-12-06T20:21:24 Z BlackC Pipes (CEOI15_pipes) C++17
100 / 100
931 ms 14896 KB
//
//  main.cpp
//  C
//
//  Created by Sajad Zare on 4/23/22.
//
 
 
#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <deque>
#include <utility>
#include <cmath>
#include <algorithm>
#include <tuple>
#include <numeric>
#include <iomanip>
#include <bitset>
using namespace std;
const int maxn=1e5+2;
int parent[maxn];
int parent2[maxn];
//int sz[maxn];
//int sz2[maxn];
bitset<maxn>seen;
int st[maxn];
//int parent3[maxnn];
int lowest[maxn];
int tmm=0;
vector<int> N[maxn];
//vector<pair<int, int>>t1;
//vector<pair<int, int>>t2;
//map<pair<int, int>, int>sss;
int x,y;
int get_root(int v){
    if (parent[v]!=v){
        parent[v]=get_root(parent[v]);
    }
    return parent[v];
}
int get_root2(int v){
    if (parent2[v]!=v){
        parent2[v]=get_root2(parent2[v]);
    }
    return parent2[v];
}
bool Union(int u,int v){
    u=get_root(u);
    v=get_root(v);
    if (u==v){
        return 1;
    }
    parent[u]=v;
    return 0;
}
bool Union2(int u,int v){
    x=u;
    y=v;
    u=get_root2(u);
    v=get_root2(v);
    if (u==v){
        return 0;
    }
    N[x].push_back(y);
    N[y].push_back(x);
    parent2[u]=v;
    return 1;
}
void dfs(int node,int pp){
    seen[node]=1;
    tmm+=1;
    st[node]=tmm;
    lowest[node]=tmm;
    //stft.push_back(node);
    for (int n: N[node]){
        if (seen[n]!=1){
            //parent3[n]=node;
            dfs(n,node);
            lowest[node]=min(lowest[node], lowest[n]);
            if (lowest[n]>st[node] && count(N[node].begin(),N[node].end(),n)==1){
                cout << node << ' ' << n << "\n";
            }
        }
        else if (n!=pp){
            lowest[node]=min(lowest[node],st[n]);
        }
    }
    //ft[node]=t;
    //stft.push_back(node);
}
//const int M = 1000000007;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m;
    cin >> n >> m;
    for (int i=1;i<n+1;i++){
        parent[i]=i;
        //sz[i]=1;
    }
    for (int i=1;i<n+1;i++){
        parent2[i]=i;
        //sz2[i]=1;
    }
    int u,v;
    for (int i=0;i<m;i++){
        cin >> u >> v;
        if (Union(u, v)){
            Union2(u, v);
        }
        else{
            N[u].push_back(v);
            N[v].push_back(u);
        }
    }
    for (int i=1;i<n+1;i++){
        if (seen[i]!=1){
            dfs(i,-1);
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 3156 KB Output is correct
2 Correct 5 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 3092 KB Output is correct
2 Correct 77 ms 2840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 132 ms 3772 KB Output is correct
2 Correct 152 ms 3284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 241 ms 5476 KB Output is correct
2 Correct 187 ms 5156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 314 ms 11288 KB Output is correct
2 Correct 281 ms 7176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 471 ms 12492 KB Output is correct
2 Correct 447 ms 9376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 632 ms 14864 KB Output is correct
2 Correct 577 ms 9144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 771 ms 14896 KB Output is correct
2 Correct 732 ms 9128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 931 ms 14236 KB Output is correct
2 Correct 855 ms 11088 KB Output is correct