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 maxn = 1e5 + 5, INF = 0x3f3f3f3f;
struct node{
int id, pai, edge, sz;
};
struct DSU{
node v[maxn];
void init(int n){
for(int i = 1; i <= n; i++) v[i] = {i, i, INF, 1};
}
int find(int x){return (v[x].pai==x ? x : find(v[x].pai));}
void join(int a, int b, int k){
a = find(a), b = find(b);
if(a==b) return;
if(v[a].sz > v[b].sz) swap(a, b);
v[a] = {a, b, k, v[a].sz};
v[b].sz += v[a].sz;
}
void query(int a, int b){
int ans = 0;
while(a!=b){
if(v[a].edge < v[b].edge) ans = max(ans, v[a].edge), a = v[a].pai;
else ans = max(ans, v[b].edge), b = v[b].pai;
}
cout << ans << '\n';
}
}dsu;
int main(){
int n, m, q; cin >> n >> m >> q;
dsu.init(n);
for(int i = m; i >= 1; i--){
for(int j = i+i; j <= n; j+=i) dsu.join(i, j, m-i+1);
}
while(q--){
int a, b; cin >> a >> b;
dsu.query(a, b);
}
}
# | 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... |