Submission #682105

#TimeUsernameProblemLanguageResultExecution timeMemory
682105MilosMilutinovicPipes (CEOI15_pipes)C++14
20 / 100
5064 ms16120 KiB
/** * author: wxhtzdy * created: 15.01.2023 19:07:48 **/ #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<int> par(n, -1); vector<vector<int>> g(n); vector<int> f(n); function<int(int)> get = [&](int x) { return par[x] < 0 ? x : par[x] = get(par[x]); }; const int L = 18; vector<int> dep(n); vector<vector<int>> jump(n, vector<int>(L)); for (int i = 0; i < n; i++) { jump[i] = vector<int>(L, i); } function<void(int, int)> Dfs = [&](int v, int pr) { dep[v] = dep[pr] + 1; jump[v][0] = pr; for (int j = 1; j < L; j++) { jump[v][j] = jump[jump[v][j - 1]][j - 1]; } for (int u : g[v]) { if (u != pr) { Dfs(u, v); } } }; auto LCA = [&](int u, int v) { if (dep[u] > dep[v]) { swap(u, v); } for (int i = L - 1; i >= 0; i--) { if (dep[jump[v][i]] >= dep[u]) { v = jump[v][i]; } } if (u == v) { return u; } for (int i = L - 1; i >= 0; i--) { if (jump[u][i] != jump[v][i]) { u = jump[u][i]; v = jump[v][i]; } } return jump[u][0]; }; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; --u; --v; assert(u != v); if (get(u) != get(v)) { int pu = get(u); int pv = get(v); if (par[pu] < par[pv]) { swap(pu, pv); swap(u, v); } Dfs(v, u); g[u].push_back(v); g[v].push_back(u); par[pu] += par[pv]; par[pv] = pu; } else { int w = LCA(u, v); f[u] += 1; f[v] += 1; f[w] -= 2; } } vector<pair<int, int>> ans; vector<bool> was(n); function<void(int, int)> Solve = [&](int v, int pr) { was[v] = true; for (int u : g[v]) { if (u != pr) { Solve(u, v); f[v] += f[u]; } } if (f[v] == 0 && pr != -1) { ans.emplace_back(v, pr); } }; for (int i = 0; i < n; i++) { if (!was[i]) { Solve(i, -1); } } for (auto& p : ans) { cout << p.first + 1 << " " << p.second + 1 << '\n'; } return 0; }
#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...