Submission #100132

# Submission time Handle Problem Language Result Execution time Memory
100132 2019-03-09T12:15:36 Z ubiratan37 Pictionary (COCI18_pictionary) C++11
140 / 140
1460 ms 9948 KB
#include <bits/stdc++.h>

#define int long long
#define double long double
#define ff first
#define ss second
#define endl '\n'
#define ii pair<int, int>
#define mp make_pair
#define mt make_tuple
#define DESYNC ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define pb push_back
#define vi vector<int>
#define vii vector< ii >
#define EPS 1e-9
#define INF 1e18
#define ROOT 1
#define M 1000000007
const double PI = acos(-1);

using namespace std;

inline int mod(int n, int m){ int ret = n%m; if(ret < 0) ret += m; return ret; }

int gcd(int a, int b){
  if(a == 0) return b;
  else return gcd(b%a, a);
}

int uf[112345];
int sz[112345];
int mx[112345];
vector<int> g[112345];
int L[112345];

int find(int u){
  if(uf[u] == u) return u;
  else return find(uf[u]);
}

void init(int n, int m){
  for(int i=0; i<=n; i++){
    uf[i] = i;
    sz[i] = 1;
    mx[i] = 0;
  }
  for(int i=m, j = 1; i >= 1; i--, j++){
     mx[i] = j;
  }
}

void merge(int x, int y){
  int a = find(x);
  int b = find(y);
  if(a == b) return;
  uf[b] = a;
  sz[a] += sz[b];
}
  
void go(int u, int l){
  L[u] = l;
  for(int v : g[u]){
    go(v, l+1);
  }
}

int32_t main(){
  DESYNC;
  int n,m;
  cin >> n >> m;
  init(n,m);
  for(int i=m; i>=1; i--){
    for(int j=i; j<=n; j+=i){
      merge(i, j);
    }
  }
  for(int i=1; i<=n; i++){
    //cout << "i + uf[i]" << i << " + " << uf[i] << endl;
    if(uf[i] != i){
      g[uf[i]].pb(i);
    }
  }
  go(find(1), 1);
  int q;
  cin >> q;
  while(q--){
    int ans = 0;
    int u,v;
    cin >> u >> v;
    while(u != v){
      if(L[u] > L[v]){
        ans = max(ans, mx[u]);
        u = uf[u];
      }
      else {
        ans = max(ans, mx[v]);
        v = uf[v];
      }
    }
    ans = max(ans, mx[u]);
    cout << ans << endl;
  }
}

# Verdict Execution time Memory Grader output
1 Correct 7 ms 3072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 3200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 3840 KB Output is correct
2 Correct 25 ms 3840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 4344 KB Output is correct
2 Correct 31 ms 4216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 129 ms 4856 KB Output is correct
2 Correct 124 ms 4860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 209 ms 5496 KB Output is correct
2 Correct 174 ms 5340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 341 ms 6428 KB Output is correct
2 Correct 385 ms 6264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 7100 KB Output is correct
2 Correct 728 ms 7988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 970 ms 8496 KB Output is correct
2 Correct 1106 ms 8952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1450 ms 9948 KB Output is correct
2 Correct 1460 ms 9720 KB Output is correct