Submission #711405

# Submission time Handle Problem Language Result Execution time Memory
711405 2023-03-16T20:28:53 Z Ahmed57 Pictionary (COCI18_pictionary) C++14
140 / 140
251 ms 40052 KB
#include<bits/stdc++.h>
using namespace std;
//DSU
#define int long long
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] = min(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 = min(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 = min({ans,ma[x][k],ma[y][k]});
            x=P[x][k],y=P[y][k];
        }
    }
    return min({ans,ma[x][0],ma[y][0]});
}
signed 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 = 0;i<=mn;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,1e9);
    while(q--){
        int a,b;cin>>a>>b;
        cout<<m-lca(a,b)+1<<endl;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 24 ms 4372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 4468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 100 ms 4676 KB Output is correct
2 Correct 106 ms 5240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 147 ms 4892 KB Output is correct
2 Correct 132 ms 5568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 12068 KB Output is correct
2 Correct 82 ms 12488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 15156 KB Output is correct
2 Correct 107 ms 17216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 138 ms 20196 KB Output is correct
2 Correct 110 ms 21592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 190 ms 25504 KB Output is correct
2 Correct 180 ms 29272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 211 ms 31312 KB Output is correct
2 Correct 223 ms 36164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 251 ms 38992 KB Output is correct
2 Correct 244 ms 40052 KB Output is correct