#include <bits/stdc++.h>
using namespace std;
#define dbg(x) cerr << #x << ": " << x << endl;
const int MAX_N = 1e5 + 5;
const int MAX_Q = 1e5 + 5;
vector<pair<int, int>> queries[MAX_N];
int ans[MAX_Q];
struct DisjointSetUnion {
vector<int> parent;
vector<unordered_set<int>> sets;
vector<int> size;
DisjointSetUnion(int n) {
parent.resize(n);
sets.resize(n);
size.resize(n);
for (int i = 0; i < n; ++i) {
parent[i] = i;
sets[i].insert(i);
size[i] = queries[i].size() + 1;
}
}
int find(int v) const {
return parent[v];
}
void unite(int u, int v, int days_n) {
u = find(u);
v = find(v);
if (u == v) return;
if (size[u] > size[v]) {
swap(u, v);
}
size[v] += sets[u].size();
for (auto x : sets[u]) {
vector<pair<int, int>> keep;
for (auto [y, idx] : queries[x]) {
if (find(y) == v) {
ans[idx] = min(ans[idx], days_n);
--size[v];
} else if (find(y) != u) {
keep.emplace_back(y, idx);
}
}
swap(queries[x], keep);
size[v] += keep.size();
parent[x] = v;
sets[v].insert(x);
}
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < q; ++i) {
int u, v;
cin >> u >> v;
queries[u].emplace_back(v, i);
queries[v].emplace_back(u, i);
}
DisjointSetUnion dsu(n + 5);
fill_n(ans, n, 1e9);
for (int g = m; g >= 1; --g) {
int u = g;
for (int v = 2 * g; v <= n; v += g) {
dsu.unite(u, v, m - g + 1);
u = v;
}
}
for (int i = 0; i < q; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2900 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
3520 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
5192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
6392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
62 ms |
10912 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
86 ms |
13712 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
125 ms |
19100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
152 ms |
21208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
238 ms |
29452 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
269 ms |
37016 KB |
Output is correct |
2 |
Correct |
262 ms |
36516 KB |
Output is correct |