Submission #669509

# Submission time Handle Problem Language Result Execution time Memory
669509 2022-12-06T15:27:41 Z arashMLG Pipes (CEOI15_pipes) C++17
100 / 100
917 ms 13388 KB
// Link: https://oj.uz/problem/view/CEOI15_pipes
// Status: NOT SOLVED

#include<bits/stdc++.h>
using namespace std;

typedef long long    ll;
typedef long double   ldb;
typedef pair<int,int> pii;
 
const int N = 1e5 + 2;
const ll mod = 1e9+7;
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define F           first
#define S           second
#define pb          push_back
#define ms(x,y)     memset((x) , (y) , sizeof (x))
#define done        return cout<<endl , 0;
#define kill(x)     cout<<x; exit(0);
#define isIn(x,s,e) ((x) >= (s) && (x) <= e)
//#define int      ll

ll pw(ll a, ll b, ll md = mod){ll res = 1; while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}

int n,m,a,b;
int par[N][2],h[N],p[N];
int dp[N];
vector<int> G[N];

int getPar(int v,int x) {
    return par[v][x] == 0 ? v : par[v][x] = getPar(par[v][x], x);
}

bool merge(int v, int u , int x){
    v  = getPar(v,x),u = getPar(u,x); if(u == v) return false; par[v][x] = u; return true;
}

void dfs(int v) {
    bool ok = false;
    for(auto u : G[v]) {
        //cerr<<"YE";
        if(h[u] == -1) {
            h[u] = h[v] + 1;
            p[u] = v;
            dfs(u);
            dp[v] += dp[u];
        } else if(h[u] < h[v] && (ok || u != p[v])) {
            dp[v] ++;
            dp[u] --;
        } else if(u == p[v]) {
            ok = true;
        }
    }
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    ms(h,-1);
    cin>>n>>m;
    for(int i = 1; i <= m; i ++) {
        cin>>a>>b;
        if(merge(a,b,0)) {
            G[a].pb(b); G[b].pb(a);
        } else if(merge(a,b,1)) {
            G[a].pb(b); G[b].pb(a);
        }
    }
    for(int i = 1; i <= n ; i++) {
        if(h[i] == -1) {
            h[i] = 0; p[i] = 0;
            dfs(i);
        }
    }
    for(int i = 1; i <= n; i ++) {
        if(dp[i] == 0 && p[i] != 0) {
            cout<<i << ' ' << p[i] <<'\n';
        }
    } 
    done;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 1 ms 3028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3536 KB Output is correct
2 Correct 6 ms 3284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 3372 KB Output is correct
2 Correct 82 ms 3240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 4044 KB Output is correct
2 Correct 155 ms 3640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 227 ms 5492 KB Output is correct
2 Correct 208 ms 5188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 314 ms 10248 KB Output is correct
2 Correct 279 ms 7448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 468 ms 11420 KB Output is correct
2 Correct 452 ms 8908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 635 ms 13344 KB Output is correct
2 Correct 574 ms 9292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 756 ms 13388 KB Output is correct
2 Correct 770 ms 9264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 917 ms 12844 KB Output is correct
2 Correct 852 ms 10364 KB Output is correct