Submission #575211

#TimeUsernameProblemLanguageResultExecution timeMemory
575211mmonteiroPictionary (COCI18_pictionary)C++17
0 / 140
186 ms29988 KiB
#include <iostream> #include <vector> #include <set> #include <utility> #include <algorithm> class DSU { public: std::vector<int> parent; std::vector<std::set<int>> sets; int n; int find(int x) { if(x == parent[x]) return x; return find(parent[x]); } void join(int a, int b) { a = find(a); b = find(b); if(a == b) { return; } if(sets[a].size() > sets[b].size()) { std::swap(a, b); } parent[a] = b; for(const int &x : sets[a]) sets[b].insert(x); } DSU(int n) { this->n = n; parent.resize(n); sets.resize(n); for(int i = 0; i < n; ++i) { parent[i] = i; sets[i].insert(i); } } }; int main(){ int n, m, q; std::cin >> n >> m >> q; std::vector<std::pair<int, int>> buc[n + 1]; for(int i = 0; i < q; ++i) { int a, b; std::cin >> a >> b; buc[a].push_back({b, i}); buc[b].push_back({a, i}); } DSU dsu(n + 1); int day = 1; std::vector<int> ans(q, m); for(int g = m; g >= 1; --g) { for(int j = g; j <= n; j += g) { int fg = dsu.find(g); int fj = dsu.find(j); if(fg != fj) { for(const auto &p : buc[j]) { if(dsu.sets[fg].count(p.first)) { ans[p.second] = std::min(ans[p.second], day); } } } dsu.join(g, j); } ++day; } for(const int &d : ans) { std::cout << d << '\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...