Submission #642028

#TimeUsernameProblemLanguageResultExecution timeMemory
642028piOOESimurgh (IOI17_simurgh)C++17
30 / 100
132 ms2200 KiB
#include <bits/stdc++.h> #include "simurgh.h" using namespace std; void my_assert(bool f) { if (!f) { cerr << "228\n"; int x = 1337; while (!f) { x *= 2; cerr << "yay"; } } } struct dsu { vector<int> p; dsu(int n = 0) { p.resize(n); iota(p.begin(), p.end(), 0); } int get(int x) { while (x != p[x]) x = p[x] = p[p[x]]; return x; } bool same(int x, int y) { return get(x) == get(y); } bool unite(int x, int y) { x = get(x), y = get(y); if (x == y) { return false; } p[y] = x; return true; } }; vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) { vector<pair<int, int>> par(n); vector<int> tin(n), tout(n); int T = 0, ans_st = 0; auto calc = [&](vector<int> st) { T = 0; ans_st = count_common_roads(st); vector<vector<pair<int, int>>> g(n); for (int i = 0; i < n - 1; ++i) { g[u[st[i]]].emplace_back(v[st[i]], i); g[v[st[i]]].emplace_back(u[st[i]], i); } function<void(int)> dfs = [&](int v) { tin[v] = T++; for (auto [to, i]: g[v]) { if (to != par[v].first) { par[to] = {v, i}; dfs(to); } } tout[v] = T; }; dfs(0); }; int m = u.size(); dsu d(n); vector<int> st; vector<bool> in_st(m); for (int i = 0; i < m; ++i) { in_st[i] = d.unite(u[i], v[i]); if (in_st[i]) { st.push_back(i); } } auto isp = [&](int x, int y) { return tin[x] <= tin[y] && tout[x] >= tout[y]; }; auto path = [&](int x, int y) -> vector<int> { int y_ = y, x_ = x; vector<int> ans; while (!isp(x, y_)) { ans.push_back(par[x].second); x = par[x].first; } while (!isp(y, x_)) { ans.push_back(par[y].second); y = par[y].first; } return ans; }; calc(st); vector<bool> sure(m); for (int i = 0; i < m; ++i) { if (find(st.begin(), st.end(), i) == st.end()) { vector<int> now = path(u[i], v[i]); vector<int> vis; bool yay = false; for (int j: now) { if (!sure[st[j]]) { auto q = st; q.erase(q.begin() + j); q.push_back(i); int val = count_common_roads(q); if (val > ans_st) { st = q; sure[i] = true; calc(st); yay = true; break; } else if (val < ans_st) { sure[st[j]] = true; break; } else { vis.push_back(st[j]); } } } if (yay) { for (int j : vis) { sure[j] = true; } } } } return st; }
#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...