#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 time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
568 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
1084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
2968 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
59 ms |
4216 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
56 ms |
7756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
60 ms |
10280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
95 ms |
14684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
137 ms |
15148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
159 ms |
23864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
186 ms |
29988 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |