답안 #711401

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
711401 2023-03-16T20:08:51 Z Ahmed57 Pictionary (COCI18_pictionary) C++14
0 / 140
268 ms 37892 KB
#include<bits/stdc++.h>
using namespace std;
//DSU
int mn = 100001;
int pr[100001];
int gs[100001];
int findleader(int x){
    if(pr[x]==x){
        return x;
    }
    return pr[x] = findleader(pr[x]);
}
bool mergegroup(int x,int y){
    int led1 = findleader(x);
    int led2 = findleader(y);
    if(led1==led2)return 0;
    if(gs[led1]>gs[led2]){
        pr[led2]=led1;
        gs[led1]+=gs[led2];
    }else{
        pr[led1]=led2;
        gs[led2]+=gs[led1];
    }
    return 1;
}
long long P[100001][18];
long long ma[100001][18];
long long hi[100001];
vector<pair<int,int>> adj[100001];
void dfs(int i,int pr,long long co){
    P[i][0] = pr;
    ma[i][0] = co;
    hi[i] = hi[pr]+1;
    for(int j = 1;j<18;j++){
        P[i][j] = P[P[i][j-1]][j-1];
        ma[i][j] = max(ma[i][j-1],ma[P[i][j-1]][j-1]);
    }
    for(auto j:adj[i]){
        if(j.first!=pr)dfs(j.first,i,j.second);
    }
}long long lca(int y,int x){
    if(hi[x]<hi[y]) swap(x,y);
    long long ans = -1e18;
    for(int k=17;k>=0;k--)
    {
        if(hi[x]-(1<<k) >= hi[y]){
            ans = max(ans,ma[x][k]);
            x=P[x][k];
        }
    }
    if(x==y) return ans;
    for(int k=17;k>=0;k--)
    {
        if(P[x][k] != P[y][k]){
            ans = max({ans,ma[x][k],ma[y][k]});
            x=P[x][k],y=P[y][k];
        }
    }
    return max({ans,ma[x][0],ma[y][0]});
}
int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    long long n,m,q;
    cin>>n>>m>>q;
    for(int i = 1;i<=n;i++){
        pr[i] = i;gs[i] =1 ;
    }
    for(int i = m;i>=1;i--){
        for(int j = i+i;j<=n;j+=i){
            if(mergegroup(i,j)){
                adj[i].push_back({j,i});
                adj[j].push_back({i,i});
            }
        }
    }
    dfs(1,1,1e18);
    while(q--){
        int a,b;cin>>a>>b;
        cout<<m-lca(a,b)+1<<endl;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 9 ms 2772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 28 ms 3040 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 102 ms 3688 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 168 ms 4120 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 80 ms 10828 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 97 ms 14200 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 165 ms 19092 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 171 ms 24876 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 226 ms 30216 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 268 ms 37892 KB Output isn't correct
2 Halted 0 ms 0 KB -