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>
using namespace std;
const int INF = 0x3f3f3f3f;
vector<int> pai, sub, edge;
int find(int v) {return ((pai[v] == v) ? v : find(pai[v]));}
void une(int a, int b, int temp)
{
a = find(a); b = find(b);
if (a == b) return;
if (sub[a] < sub[b]) swap(a, b);
pai[b] = a;
sub[a] += sub[b];
edge[b] = temp;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, M, Q;
cin >> N >> M >> Q;
pai.resize(N+1); edge.resize(N+1); sub.resize(N+1);
iota(pai.begin(), pai.end(), 0);
fill(sub.begin(), sub.end(), 1);
fill(edge.begin(), edge.end(), INF);
for (int i = M; i >= 1; i--)
{
for (int k = 2; k*i <= N; k++) une(i, k*i, M - i + 1);
}
// for (int i = 1; i <= N; i++) cerr << "||" << i << " " << pai[i] << " " << edge[i] << '\n';
while (Q--)
{
int X, Y;
cin >> X >> Y;
int dist = -1;
while (X != Y)
{
if (sub[X] > sub[Y]) {dist = max(dist, edge[Y]); Y = pai[Y];}
else {dist = max(dist, edge[X]); X = pai[X];}
}
cout << dist << '\n';
}
}
# | 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... |