Submission #873564

# Submission time Handle Problem Language Result Execution time Memory
873564 2023-11-15T10:01:42 Z Ghulam_Junaid Pipes (CEOI15_pipes) C++17
90 / 100
827 ms 22004 KB
#include <bits/stdc++.h>
using namespace std;
 
const int N = 1e5 + 1;
int n, m, h[N], mnh[N], edges, u, v, i, r1, r2;
bool vis[N];
vector<pair<int, int>> g[N];
 
int root1(int& v){
    return (h[v] == -1 ? v : h[v] = root1(h[v]));
}
 
int root2(int& v){
    return (mnh[v] == -1 ? v : mnh[v] = root2(mnh[v]));
}
 
void merge1(int& u, int& v){    
    r1 = root1(v);
    r2 = root1(u);
 
    h[r1] = r2;
 
    edges++;
    g[v].push_back({u, edges});
    g[u].push_back({v, edges});
}
 
void merge2(int& u, int& v){
    r1 = root2(u);
    r2 = root2(v);
 
    mnh[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(){
 
    scanf("%d%d", &n, &m);
 
    for (i=1; i<=n; i++)
        h[i] = mnh[i] = -1;
 
    for (i=0; i<m; i++){
        scanf("%d%d", &u, &v);
 
        if (root1(u) != root1(v))
            merge1(u, v);
        else if (root2(u) != root2(v))
            merge2(u, v);
    }
 
    for (i=1; i<=n; i++)
        h[i] = mnh[i] = n + 100;
 
    for (v = 1; v <= n; v++)
        if (!vis[v])
            dfs(v);
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:66:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         scanf("%d%d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3164 KB Output is correct
2 Correct 4 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 3232 KB Output is correct
2 Correct 71 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 130 ms 3688 KB Output is correct
2 Correct 141 ms 3432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 221 ms 5428 KB Output is correct
2 Correct 184 ms 5056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 270 ms 10936 KB Output is correct
2 Correct 234 ms 8020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 447 ms 12108 KB Output is correct
2 Correct 402 ms 8644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 557 ms 14400 KB Output is correct
2 Correct 553 ms 10328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 690 ms 14404 KB Output is correct
2 Correct 643 ms 10372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 812 ms 13828 KB Output is correct
2 Runtime error 827 ms 22004 KB Memory limit exceeded
3 Halted 0 ms 0 KB -