Submission #362694

# Submission time Handle Problem Language Result Execution time Memory
362694 2021-02-04T06:27:59 Z ijxjdjd Pipes (CEOI15_pipes) C++17
90 / 100
3669 ms 29808 KB
#pragma GCC target("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
#define FR(i, N) for (int i = 0; i < int(N); i++)
#define all(x) begin(x), end(x)

using namespace std;

using ll = long long;

const int MAXN = 100000;
const int MAXK = 17;
int up[MAXN][MAXK];
int sm[MAXN];
int rt[MAXN];
int sz[MAXN];
bool bad[MAXN];
int depth[MAXN];
vector<int> ch[MAXN];
int cnt = 0;
void makeBad(int n, int p) {
    for (int e : ch[n]) {
        if (e != p) {
            makeBad(e, n);
            sm[n] += sm[e];
        }
    }
    if (sm[n] > 0) {
        bad[n] = true;
    }
}
void reset(int n, int p, int d, int o) {
    up[n][0] = p;
    sm[n] = 0;
    rt[n] = o;
    depth[n] = d;
    for (int i = 1; i < MAXK; i++) {
        up[n][i] = up[up[n][i-1]][i-1];
    }
    for (int e : ch[n]) {
        if (e != p) {
            reset(e, n, d+1, o);
        }
    }
}
int getUp(int u, int k) {
    for (int i = 0; i < MAXK; i++) {
        if (k&(1<<i)) {
            u = up[u][i];
        }
    }
    return u;
}
int lca(int u, int v) {
    if (depth[u] > depth[v]) {
        swap(u, v);
    }
    v = getUp(v, depth[v] - depth[u]);
    if (u == v) {
        return u;
    }
    for (int k = MAXK-1; k >= 0; k--) {
        if (up[u][k] != up[v][k]) {
            u = up[u][k];
            v = up[v][k];
        }
    }
    return up[u][0];
}
int main() {
	cin.tie(0);
	cin.sync_with_stdio(0);
	int N, M;
	cin >> N >> M;
	FR(i, N) {
        up[i][0] = -1;
	}
	FR(i, M) {
	    int u, v;
        cin >> u >> v;
        u--, v--;
        if (up[u][0] == -1 && up[v][0] == -1) {
            ch[u].push_back(v);
            ch[v].push_back(u);
            reset(u, u, 0, u);
            sz[u] = 2;
        }
        else {
            if (up[u][0] == -1 || up[v][0] == -1) {
                ch[u].push_back(v);
                ch[v].push_back(u);
                if (up[v][0] == -1) {
                    swap(u, v);
                }
                reset(u, v, depth[v]+1, rt[v]);
                sz[rt[v]]++;
            }
            else {
                if (rt[u] == rt[v]) {
                    int x = lca(u, v);
                    sm[x] -= 2;
                    sm[u]++;
                    sm[v]++;
                }
                else {
                    if (sz[rt[u]] > sz[rt[v]]) {
                        swap(u, v);
                    }
                    int j = u;
                    bool last = false;
                    makeBad(rt[u], rt[u]);
                    while (true) {
                        swap(bad[j], last);
                        if (j == up[j][0]) {
                            break;
                        }
                        j = up[j][0];
                    }
                    ch[u].push_back(v);
                    ch[v].push_back(u);
                    sz[rt[v]] += sz[rt[u]];
                    reset(u, v, depth[v]+1, rt[v]);
                }
            }
        }
	}
	FR(i, N) {
	    if (up[i][0] == i) {
            makeBad(i, i);
	    }
	}
	FR(i, N) {
        if (up[i][0] != i && up[i][0] != -1 && !bad[i]) {
            cout << i+1 <<  " " << up[i][0] + 1 << '\n';
        }
	}
	return 0;
}

Compilation message

pipes.cpp:2: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    2 | #pragma GCC optimization ("O3")
      | 
pipes.cpp:3: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    3 | #pragma GCC optimization ("unroll-loops")
      |
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 3 ms 2668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 3308 KB Output is correct
2 Correct 7 ms 3308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 174 ms 3924 KB Output is correct
2 Correct 164 ms 3948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 324 ms 4716 KB Output is correct
2 Correct 360 ms 4612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 553 ms 6124 KB Output is correct
2 Correct 458 ms 6636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1057 ms 10924 KB Output is correct
2 Correct 940 ms 10732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1679 ms 12860 KB Output is correct
2 Correct 1346 ms 12308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2482 ms 14816 KB Output is correct
2 Runtime error 2366 ms 29808 KB Memory limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 3022 ms 14188 KB Output is correct
2 Correct 3030 ms 14188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3669 ms 13676 KB Output is correct
2 Correct 2876 ms 14188 KB Output is correct