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;
#define MAXN 100005
const int INF = 0x3f3f3f3f;
int p[MAXN], pp[MAXN], w[MAXN];
int n, m, q;
int find(int x){
if(p[x]==x) return x;
return find(p[x]);
}
void join(int x, int y, int t){
x=find(x);
y=find(y);
if(x==y) return;
if(w[x] > w[y]) swap(x, y);
p[x] = y;
pp[x] = t;
w[y] += w[x];
}
void solve( int id ){
int k = m - id + 1;
for( int i = 2*k; i <= n; i += k ) join(k, i, id );
}
int main(){
ios_base::sync_with_stdio(false); cout.tie(NULL);
cin >> n >> m >> q;
for(int i = 1; i<=n; i++){
p[i] = i;
pp[i] = INF;
w[i] = 1;
}
for(int i = 1; i<=m; i++) solve(i);
for(int i = 0; i<q; i++){
int a, b; cin >> a >> b;
int res = 0;
while(a != b){
if(pp[a] > pp[b]) swap(a, b);
res = max(res, pp[a]);
a = p[a];
}
cout << res << 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... |