#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, INT_MAX);
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;
}
Compilation message
pictionary.cpp: In function 'int main()':
pictionary.cpp:64:29: error: 'INT_MAX' was not declared in this scope
64 | std::vector<int> ans(q, INT_MAX);
| ^~~~~~~
pictionary.cpp:6:1: note: 'INT_MAX' is defined in header '<climits>'; did you forget to '#include <climits>'?
5 | #include <algorithm>
+++ |+#include <climits>
6 |