Submission #379861

# Submission time Handle Problem Language Result Execution time Memory
379861 2021-03-19T14:40:23 Z parsabahrami Pipes (CEOI15_pipes) C++17
90 / 100
4217 ms 15188 KB
// To Hell and Back
#include <bits/stdc++.h>
#pragma GCC optimize("O2")

using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
 
#define SZ(x)                       (int) x.size()
#define F                           first
#define S                           second

const int N = 1e5 + 10;
int ps[N], rt[N], S[N], P[8][N], H[N], n, m; 
vector<int> adj[N];

int Find(int v) {
    return !rt[v] ? v : rt[v] = Find(rt[v]);
}

int LCA(int u, int v) {
    if (H[u] < H[v]) swap(u, v);
    for (int i = H[u] - H[v], j = 0; i; i /= 5, j++) {
        while (i % 5) u = P[j][u], i--;
    }
    if (u == v) return u;
    for (int i = 7; ~i; i--) {
        for (int j = 0; j < 4; j++)
            if (P[i][u] != P[i][v])
                u = P[i][u], v = P[i][v];
    }
    return P[0][v];
}

void upd(int v, int p) {
    P[0][v] = p; H[v] = H[p] + 1;
    for (int i = 1; i < 8; i++) {
        P[i][v] = P[i - 1][P[i - 1][P[i - 1][P[i - 1][P[i - 1][v]]]]];
    }	
    for (int u : adj[v]) 
        if (u != p) upd(u, v);
}

void Union(int u, int v) {
    int pu = Find(u), pv = Find(v);
    if (pu == pv) {
        ps[u]++, ps[v]++, ps[LCA(u, v)] -= 2;
    } else {
        if (S[pv] < S[pu]) swap(pu, pv), swap(u, v);
        S[pv] += S[pu]; rt[pu] = pv;
        adj[u].push_back(v), adj[v].push_back(u);
        upd(u, v);
    }
}

void DFS(int v, int p = -1) {
    S[v] = 1;
    for (int u : adj[v]) 
        if (u != p) {
            DFS(u, v), ps[v] += ps[u];
            if (!ps[u]) printf("%d %d\n", u, v);
        }
}

int main() {
    fill(S, S + N, 1);
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++) {
        int u, v; scanf("%d%d", &u, &v);
        Union(u, v);
    }
    memset(S, 0, sizeof S);
    for (int i = 1; i <= n; i++) 
        if (!S[i]) DFS(i);
    return 0;
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:68:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   68 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:70:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   70 |         int u, v; scanf("%d%d", &u, &v);
      |                   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3052 KB Output is correct
2 Incorrect 3 ms 3052 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 8 ms 3564 KB Output is correct
2 Correct 8 ms 3564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 226 ms 5484 KB Output is correct
2 Correct 218 ms 5484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 435 ms 6568 KB Output is correct
2 Correct 498 ms 5996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 810 ms 9452 KB Output is correct
2 Correct 627 ms 8428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1201 ms 12756 KB Output is correct
2 Correct 1038 ms 12652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1859 ms 11492 KB Output is correct
2 Correct 1698 ms 11488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2664 ms 15188 KB Output is correct
2 Correct 2744 ms 13036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3277 ms 12728 KB Output is correct
2 Correct 3423 ms 13128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4217 ms 13524 KB Output is correct
2 Correct 3582 ms 13848 KB Output is correct