이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int par[300000], siz[300000];
int get_parent(int v){
if(v == par[v]){
return v;
}
return par[v] = get_parent(par[v]);
}
void merge(int u, int v){
int parent_u = get_parent(u);
int parent_v = get_parent(v);
if(parent_u == parent_v) return;
if(siz[parent_u] < siz[parent_v]){
swap(parent_u, parent_v);
}
par[parent_v] = parent_u;
siz[parent_u] += siz[parent_v];
}
bool ask(int n, int m, int j, int u, int v){
for(int i = 0; i <= n; i++){
par[i] = i;
siz[i] = 1;
}
for(int i = 1; i <= j; i++){
int x = m - i + 1;
for(int y = 2 * x; y <= n; y += x){
merge(x, y);
}
}
if(get_parent(u) == get_parent(v)){
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
for(int i = 0; i < q; i++){
int u, v;
cin >> u >> v;
int l = 1, r = m;
int opt = m;
while(l <= r){
int md = (l + r) / 2;
if(ask(n, m, md, u, v)){
opt = md, r = md - 1;
} else {
l = md + 1;
}
}
cout << opt << "\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... |