답안 #873148

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
873148 2023-11-14T14:21:14 Z Ghulam_Junaid Pipes (CEOI15_pipes) C++17
80 / 100
850 ms 65536 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;
int par1[N], par2[N], h[N], mnh[N], edges;
bool vis[N];
vector<pair<int, int>> g[N];

int root1(int v){
    return (par1[v] == -1 ? v : par1[v] = root1(par1[v]));
}

int root2(int v){
    return (par2[v] == -1 ? v : par2[v] = root2(par2[v]));
}

void merge1(int u, int v){    
    int r1 = root1(v);
    int r2 = root1(u);

    par1[r1] = r2;

    edges++;
    g[v].push_back({u, edges});
    g[u].push_back({v, edges});
}

void merge2(int u, int v){
    int r1 = root2(u);
    int r2 = root2(v);

    if (r1 == r2)
        return;

    par2[r1] = r2;

    edges++;
    g[v].push_back({u, edges});
    g[u].push_back({v, edges});
}

void dfs(int v, pair<int, int> prev = {-1, -1}){
    vis[v] = 1;
    if (prev.first == -1)
        h[v] = 0;
    mnh[v] = h[v];

    for (auto [u, id] : g[v]){
         if (prev.first == u and prev.second == id)
            continue;

        mnh[v] = min(mnh[v], h[u]);

        if (vis[u])
            continue;

        h[u] = h[v] + 1;
        dfs(u, {v, id});
        mnh[v] = min(mnh[v], mnh[u]);

        if (mnh[u] >= h[u])
            printf("%d %d\n", u, v);
            
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n, m;
    scanf("%d%d", &n, &m);

    for (int i=1; i<=n; i++){
        par1[i] = par2[i] = -1;
        mnh[i] = n + 100;
        h[i] = n + 100;
    }

    for (int i=0; i<m; i++){
        int u, v;
        scanf("%d%d", &u, &v);

        if (root1(u) != root1(v))
            merge1(u, v);
        else if (root2(u) != root2(v))
            merge2(u, v);
    }

    for (int v = 1; v <= n; v++)
        if (!vis[v])
            dfs(v);
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:72:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:82:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |         scanf("%d%d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 3420 KB Output is correct
2 Correct 3 ms 3164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 3104 KB Output is correct
2 Correct 70 ms 3096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 118 ms 3924 KB Output is correct
2 Correct 138 ms 3564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 204 ms 5892 KB Output is correct
2 Correct 168 ms 5308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 278 ms 12388 KB Output is correct
2 Correct 250 ms 8612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 413 ms 13516 KB Output is correct
2 Correct 392 ms 9808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 581 ms 16180 KB Output is correct
2 Correct 536 ms 11144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 665 ms 15964 KB Output is correct
2 Runtime error 657 ms 64080 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 803 ms 15252 KB Output is correct
2 Runtime error 850 ms 65536 KB Memory limit exceeded
3 Halted 0 ms 0 KB -