#include <bits/stdc++.h>
#define MAX (int)(1e6)
#define INF (int)(1e8)
using namespace std;
int n, m, q, a, b, pai[MAX], t, tam[MAX], tempo[MAX];
int find(int x)
{
if (x==pai[x]) return x;
return find(pai[x]);
}
void join(int a, int b)
{
int A = find(a), B = find(b);
// cout << a << ' ' << b << endl;
if (A==B) return;
if (tam[A] > tam[B]) swap(A, B);
tam[B] += tam[A];
pai[A] = B;
tempo[A] = t;
}
int query(int a, int b)
{
int resp = 1;
while (a != b)
{
// cout << a << ' ' << b << endl;
// cout << tempo[a] << ' ' << tempo[b] << endl;
if (tempo[a] < tempo[b]) resp = max(resp, tempo[a]), a = pai[a];
else resp = max(resp, tempo[b]), b = pai[b];
}
return resp;
}
int main()
{
cin >> n >> m >> q;
for (int i = 1; i <= n; i++)
tempo[i] = INF, pai[i] = i, tam[i] = 1;
for (int i = m; i >= 1; i--)
{
++t;
for (int j = i; j <= n; j += i)
join(i, j);
}
// for (int i = 1; i <= n; i++)
// cout << tempo[i] << endl;
while (q--)
{
cin >> a >> b;
cout << query(a, b) << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
1200 KB |
Output is correct |
2 |
Correct |
115 ms |
1068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
167 ms |
1580 KB |
Output is correct |
2 |
Correct |
148 ms |
1412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
1300 KB |
Output is correct |
2 |
Correct |
82 ms |
1204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
1472 KB |
Output is correct |
2 |
Correct |
94 ms |
1532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
138 ms |
2004 KB |
Output is correct |
2 |
Correct |
88 ms |
1604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
156 ms |
2328 KB |
Output is correct |
2 |
Correct |
165 ms |
2524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
179 ms |
2832 KB |
Output is correct |
2 |
Correct |
175 ms |
2920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
201 ms |
3336 KB |
Output is correct |
2 |
Correct |
191 ms |
3232 KB |
Output is correct |