Submission #379888

# Submission time Handle Problem Language Result Execution time Memory
379888 2021-03-19T15:46:57 Z parsabahrami Pipes (CEOI15_pipes) C++17
100 / 100
4030 ms 13144 KB
    // To Hell and Back
    #include <bits/stdc++.h>
     
    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], from[N], to[N], P[9][N], H[N], n, m; 
    vector<int> adj[N]; vector<pii> vec;
     
    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 = 8; ~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 < 9; 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 DFS(int v, int p = -1) {
        for (int u : adj[v]) 
            if (u != p) {
                DFS(u, v), ps[v] += ps[u];
                if (ps[u]) vec.push_back({min(u, v), max(u, v)});
                ps[u] = 0;
            }
    }
     
    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;
            DFS(pu), ps[pu] = 0;
            adj[u].push_back(v), adj[v].push_back(u);
            upd(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);
        }
        DFS(Find(1)); sort(vec.begin(), vec.end());
        for (int i = 1; i <= n; i++) {
            for (int u : adj[i]) { 
                if (u < i) continue;
                auto it = lower_bound(vec.begin(), vec.end(), pii(i, u));
                if (it != vec.end() && *it == pii(i, u)) continue;
                printf("%d %d\n", i, u);
            }
        }
        return 0;
    }

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:68:14: 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:28: 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 2 ms 3052 KB Output is correct
2 Correct 2 ms 3052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 3564 KB Output is correct
2 Correct 9 ms 3564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 225 ms 3436 KB Output is correct
2 Correct 219 ms 3564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 432 ms 4056 KB Output is correct
2 Correct 487 ms 4332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 740 ms 5484 KB Output is correct
2 Correct 626 ms 5868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1220 ms 9708 KB Output is correct
2 Correct 1016 ms 10884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1921 ms 10736 KB Output is correct
2 Correct 1677 ms 10768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2941 ms 12072 KB Output is correct
2 Correct 2785 ms 13088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3560 ms 12072 KB Output is correct
2 Correct 3364 ms 13144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4030 ms 11792 KB Output is correct
2 Correct 3623 ms 12264 KB Output is correct