#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int nax = 100'005;
struct Edge {
int to, w;
};
int parent[nax];
int sz[nax];
int find(int v) {
if (parent[v] == v) {
return v;
}
return parent[v] = find(parent[v]);
}
bool unite(int a, int b) {
a = find(a), b = find(b);
if (a != b) {
if (sz[a] < sz[b]) swap(a, b);
parent[b] = a;
sz[a] += sz[b];
return true;
}
return false;
}
vector<Edge> adj[nax];
int par[nax], depth[nax], cost[nax];
void dfs(int v) {
for(Edge u : adj[v]) {
if (u.to == par[v]) continue;
par[u.to] = v;
depth[u.to] = depth[v] + 1;
cost[u.to] = u.w;
dfs(u.to);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, q;
cin >> n >> m >> q;
for(int i = 1; i <= n; i++) {
parent[i] = i;
sz[i] = 1;
}
for(int i = m; i >= 1; i--) {
for(int j = i+i; j <= n; j+=i) {
if (unite(i, j)) {
adj[i].push_back({j, m-i+1});
adj[j].push_back({i, m-i+1});
// cout << i << "->" << j << " " << m-i+1 << "\n";
}
}
}
par[1] = -1;
depth[1] = 0;
cost[1] = 0;
dfs(1);
while(q--) {
int x, y;
cin >> x >> y;
int dx = depth[x], dy = depth[y];
int mx = 0;
while(dx > dy) {
mx = max(mx, cost[x]);
x = par[x];
dx--;
}
while(dy > dx) {
mx = max(mx, cost[y]);
y = par[y];
dy--;
}
while(x != y) {
mx = max({mx, cost[x], cost[y]});
x = par[x];
y = par[y];
}
cout << mx << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
3504 KB |
Output is correct |
2 |
Correct |
23 ms |
3532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
3936 KB |
Output is correct |
2 |
Correct |
31 ms |
3840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
4624 KB |
Output is correct |
2 |
Correct |
33 ms |
4672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
5464 KB |
Output is correct |
2 |
Correct |
29 ms |
5628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
6440 KB |
Output is correct |
2 |
Correct |
49 ms |
6212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
7772 KB |
Output is correct |
2 |
Correct |
73 ms |
7964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
8640 KB |
Output is correct |
2 |
Correct |
115 ms |
9420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
111 ms |
10004 KB |
Output is correct |
2 |
Correct |
112 ms |
10188 KB |
Output is correct |