#include <bits/stdc++.h>
using namespace std;
using ii = pair<int, int>;
template <int maxn>
struct Dsu {
int n;
int pai[maxn], w[maxn];
int find(int x)
{
if (x == pai[x]) return x;
return pai[x] = find(pai[x]);
}
bool find(int a, int b)
{
a = find(a), b= find(b);
return a == b;
}
void join(int a, int b)
{
a = find(a), b = find(b);
if (w[a] < w[b]) swap(a, b);
pai[b] = a;
w[a] += w[b];
}
void init(int n_) {
n = n_;
fill(w+1, w+n+1, 1);
iota(pai+1, pai+n+1, 1);
}
};
int const maxn = 101010;
struct Query
{
int ff, ss, id;
};
Dsu<maxn> dsu;
Query qr[maxn];
bool vis[maxn];
int res[maxn];
int main()
{
ios::sync_with_stdio(false), cin.tie(0);
int n, m;
int q;
cin >> n >> m >> q;
for (int i = 0; i < q; i++) {
cin >> qr[i].ff >> qr[i].ss;
qr[i].id = i;
}
dsu.init(n);
bool ok = false;
for (int k = m; k >= 1; k--) {
for (int i = k*2; i <= n; i += k) {
if (!dsu.find(k, i)) dsu.join(k, i);
}
for (int i = 0; i < q; i++) {
if (!vis[i] and dsu.find(qr[i].ff, qr[i].ss)) {
vis[i] = true;
res[i] = m-k+1;
}
}
}
for (int i = 0; i < q; i++)
cout << res[i] << "\n";
}
Compilation message
pictionary.cpp: In function 'int main()':
pictionary.cpp:68:10: warning: unused variable 'ok' [-Wunused-variable]
bool ok = false;
^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
260 ms |
1792 KB |
Output is correct |
2 |
Correct |
54 ms |
1880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
378 ms |
2604 KB |
Output is correct |
2 |
Correct |
185 ms |
2604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1554 ms |
2604 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1578 ms |
2604 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1562 ms |
2604 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
139 ms |
2924 KB |
Output is correct |
2 |
Execution timed out |
1555 ms |
3500 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1565 ms |
3500 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1587 ms |
3544 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |