This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |