Submission #382566

# Submission time Handle Problem Language Result Execution time Memory
382566 2021-03-27T17:38:47 Z Mahdi_Shokoufi Pipes (CEOI15_pipes) C++17
100 / 100
1395 ms 14444 KB
//In the name of Allah
#include <bits/stdc++.h>

using namespace std;

#define pb push_back

const int N = 1e5 + 10;

int par[2][N], mark[N], t;
vector < int > adj[N];

int getPar(int v){
    return par[t][v] == v ? v : par[t][v] = getPar(par[t][v]);
}

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

void DFS(int v, int p = 0){
    mark[v] = 1;
    par[1][v] = N;
    int cnt = 0;
    for (int u : adj[v]){
        if (u == p){
            cnt ++;
            continue;
        }
        if (mark[u])
            par[1][v] = min(par[1][v], par[0][u]);
        else{
            par[0][u] = par[0][v] + 1;
            DFS(u, v);
            par[1][v] = min(par[1][v], par[1][u]);
        }
    }
    if (p && cnt == 1 && par[0][p] < par[1][v])
        cout << p << ' ' << v << '\n';
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < N; i ++)
        par[0][i] = par[1][i] = i;
    for (int i = 0; i < m; i ++){
        int u, v, f = 1;
        cin >> u >> v;
        t = 0;
        if (merge(u, v)){
            adj[u].pb(v);
            adj[v].pb(u);
            f = 0;
        }
        t = 1;
        if (f && merge(u, v)){
            adj[u].pb(v);
            adj[v].pb(u);
        }
    }
    memset(par, 0, sizeof par);
    for (int v = 1; v <= n; v ++)
        if (!mark[v])
            DFS(v);
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3456 KB Output is correct
2 Correct 3 ms 3436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 4076 KB Output is correct
2 Correct 7 ms 3820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 117 ms 3948 KB Output is correct
2 Correct 115 ms 3948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 202 ms 4992 KB Output is correct
2 Correct 250 ms 4204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 326 ms 6124 KB Output is correct
2 Correct 291 ms 6252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 498 ms 11116 KB Output is correct
2 Correct 409 ms 7788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 703 ms 11928 KB Output is correct
2 Correct 671 ms 9324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 960 ms 13804 KB Output is correct
2 Correct 922 ms 8940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1243 ms 14004 KB Output is correct
2 Correct 1092 ms 9296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1395 ms 14444 KB Output is correct
2 Correct 1279 ms 11832 KB Output is correct