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 N = 1e5 + 10;
int n, m, q, ans[N], par[N], tme;
vector<int> query[N], S[N];
vector<pair<int, int>> vec;
int root(int v){
return (par[v] == -1 ? v : root(par[v]));
}
void merge(int u, int v){
if ((u = root(u)) == (v = root(v)))
return ;
if (S[u].size() > S[v].size())
swap(u, v);
par[u] = v;
for (int a : S[u]){
S[v].push_back(a);
for (int id : query[a]){
int b = vec[id].first + vec[id].second - a;
if (root(b) == v)
ans[id] = min(ans[id], tme);
}
}
S[u].clear();
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
memset(ans, 31, sizeof ans);
cin >> n >> m >> q;
for (int i = 1; i <= n; i ++){
par[i] = -1;
S[i] = {i};
}
for (int i = 0; i < q; i ++){
int a, b;
cin >> a >> b;
query[a].push_back(i);
query[b].push_back(i);
vec.push_back({a, b});
}
for (int g = m; g > 0; g --){
tme++;
for (int i = 2 * g; i <= n; i += g)
merge(i, i - g);
}
for (int i = 0; i < q; i ++)
cout << ans[i] << '\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... |