#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
using namespace std;
const int MAXN = 1e5+5;
int sz[MAXN], par[MAXN];
int get(int x) {
return x == par[x] ? x : par[x] = get(par[x]);
}
void unite(int a, int b) {
a = get(a), b = get(b);
if (a == b) {
return;
}
if (sz[a] > sz[b]) {
swap(a, b);
}
par[a] = b;
sz[b] += sz[a];
}
int main() {
int n, m, q;
cin >> n >> m >> q;
vector<pair<pair<int, int>, int>> queries;
int ans[MAXN];
for (int i = 0; i < MAXN; i++) {
sz[i] = 1;
par[i] = i;
ans[i] = -1;
}
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
queries.push_back({ { a, b }, i });
}
for (int i = 0; i < m; i++) {
for (int j = m - i; j <= n; j += (m - i)) {
unite(m-i, j);
for (auto k : queries) {
if (ans[k.second] == -1) {
if (get(k.first.first) == get(k.first.second)) {
ans[k.second] = i + 1;
}
}
}
}
}
for (int i = 0; i < q; i++) {
cout << ans[i] << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
1664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
477 ms |
1984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1592 ms |
3180 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1591 ms |
3184 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1582 ms |
2416 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1554 ms |
2420 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1594 ms |
3180 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1593 ms |
3184 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1589 ms |
3180 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1583 ms |
3184 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |