Advertisement
happy_nesquik

Untitled

Dec 10th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define _ ios_base::sync_with_stdio(0);cin.tie(0);
  6. #define pb push_back
  7.  
  8. int id[100100];
  9. int sz[100100];
  10.  
  11. int find(int x){
  12.     if(x == id[x]) return x;
  13.     return id[x] = find(id[x]);
  14. }
  15.  
  16. void uni(int p, int q){
  17.     p = find(p);
  18.     q = find(q);
  19.     if(p==q) return;
  20.     if(sz[p]>sz[q]) swap(p, q);
  21.     id[p] = q;
  22.     sz[q] += sz[p];
  23.     return;
  24. }
  25.  
  26. int main(){ _
  27.     int n, m, q;
  28.     int ans[100100], a[100100], b[100100];
  29.    
  30.     cin >> n >> m >> q;
  31.  
  32.     for(int i=0; i<=n; i++){id[i] = i, sz[i]=1;}
  33.  
  34.     for(int i=0; i<q; i++){
  35.         cin >> a[i] >> b[i];
  36.         ans[i] = m;
  37.     }
  38.  
  39.     for(int i=m; i>0; i--){
  40.         for(int j=i+i; j<=n; j+=i){
  41.             uni(i, j);
  42.         }
  43.         for(int j=0; j<q; j++){
  44.             if(ans[j] == m and find(a[j]) == find(b[j])){
  45.             //  cout << find(a[j]) << " " << find(b[j]) << endl;
  46.                 ans[j] = m-i+1;
  47.             }
  48.         }
  49.     }
  50.  
  51.     for(int i=0; i<q; i++)
  52.         cout << ans[i] << endl;
  53.    
  54.  
  55.     exit(0);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement