Submission #362694

#TimeUsernameProblemLanguageResultExecution timeMemory
362694ijxjdjdPipes (CEOI15_pipes)C++17
90 / 100
3669 ms29808 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...