Submission #217119

# Submission time Handle Problem Language Result Execution time Memory
217119 2020-03-29T04:26:35 Z quocnguyen1012 Pictionary (COCI18_pictionary) C++14
140 / 140
55 ms 3320 KB
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back

using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

const int maxn = 1e5 + 5;

int sz[maxn], par[maxn], edge[maxn];
int N, M, Q;

void init(int n)
{
  for(int i = 1; i <= n; ++i){
    sz[i] = 1;
    par[i] = i;
    edge[i] = 1e9;
  }
}

int finds(int u)
{
  if(par[u] == u) return u;
  return finds(par[u]);
}

void merges(int u, int v, int w)
{
  u = finds(u); v = finds(v);
  if(u == v) return;
  if(sz[u] < sz[v]) swap(u, v);
  par[v] = u;
  edge[v] = w;
  sz[u] += sz[v];
}

int path(int u, int v)
{
  int ans = 0;
  while(u != v){
    //cerr << u << ' ' << v << '\n';
    if(edge[u] < edge[v]){
      ans = max(ans, edge[u]);
      u = par[u];
    }
    else{
      ans = max(ans, edge[v]);
      v = par[v];
    }
  }
  return ans;
}

signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #ifdef LOCAL
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  #endif // LOCAL
  cin >> N >> M >> Q;
  init(N);
  for(int i = M; i >= 1; --i){
    for(int j = i; j <= N; j += i){
      merges(i, j, M - i + 1);
    }
  }
  while(Q--){
    int u, v; cin >> u >> v;
    cout << path(u, v) << '\n';
  }
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 640 KB Output is correct
2 Correct 25 ms 1408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 760 KB Output is correct
2 Correct 32 ms 1572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 896 KB Output is correct
2 Correct 22 ms 1408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 1024 KB Output is correct
2 Correct 25 ms 1664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 1272 KB Output is correct
2 Correct 33 ms 1792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 1400 KB Output is correct
2 Correct 45 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 1784 KB Output is correct
2 Correct 49 ms 3064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 2168 KB Output is correct
2 Correct 55 ms 3320 KB Output is correct