Submission #711402

# Submission time Handle Problem Language Result Execution time Memory
711402 2023-03-16T20:10:30 Z Ahmed57 Pictionary (COCI18_pictionary) C++14
0 / 140
233 ms 38984 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] = 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]});
}
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,1e18);
    while(q--){
        int a,b;cin>>a>>b;
        cout<<m-lca(a,b)+1<<endl;
    }
}
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 4308 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 28 ms 4436 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 96 ms 4648 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 144 ms 4808 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 81 ms 11984 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 102 ms 15236 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 136 ms 20172 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 196 ms 25492 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 205 ms 31304 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 233 ms 38984 KB Output isn't correct
2 Halted 0 ms 0 KB -