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 + 10;
struct pergunta{
int ini,fim,resp,meio,u,v,id;
pergunta(int _ini,int _fim,int _u,int _v,int _id){
ini = _ini;
fim = _fim;
resp = -1;
u = _u;
v = _v;
id = _id;
}
bool operator<(const pergunta &other)const{
return meio > other.meio;
}
};
vector<pergunta> P;
int N,M,Q,pai[MAXN],peso[MAXN],resposta[MAXN];
void clear(){
for(int i = 1;i<=N;i++){
pai[i] = i;
peso[i] = 1;
}
}
int find(int x){
if(x == pai[x]) return x;
return pai[x] = find(pai[x]);
}
void join(int x,int y){
x = find(x);
y = find(y);
if(x == y) return;
if(peso[x] < peso[y]) swap(x,y);
pai[y] = x;
peso[x] += peso[y];
}
void uniao(int v){
for(int u = 2*v;u<=N;u+=v){
join(u,v);
}
}
int main(){
scanf("%d %d %d",&N,&M,&Q);
for(int i = 1;i<=Q;i++){
int u,v;
scanf("%d %d",&u,&v);
pergunta davez(1,M,u,v,i);
P.push_back(davez);
}
for(int iteracao = 1;iteracao<=17;iteracao++){
clear();
for(int i = 0;i<Q;i++){
if(P[i].ini > P[i].fim){
P[i].meio = -1;
}
else{
P[i].meio = (P[i].ini + P[i].fim)/2;
}
}
sort(P.begin(),P.end());
int atual = M;
for(int i = 0;i<Q;i++){
if(P[i].meio == -1) continue;
while(atual >= P[i].meio){
uniao(atual);
atual--;
}
if(find(P[i].u) == find(P[i].v)){
P[i].resp = P[i].meio;
P[i].ini = P[i].meio + 1;
}
else{
P[i].fim = P[i].meio - 1;
}
}
}
for(int i = 0;i<Q;i++) resposta[P[i].id] = abs(P[i].resp - M) + 1;
for(int i = 1;i<=Q;i++) printf("%d\n",resposta[i]);
return 0;
}
Compilation message (stderr)
pictionary.cpp: In function 'int main()':
pictionary.cpp:44:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&N,&M,&Q);
^
pictionary.cpp:47:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&u,&v);
^
# | 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... |