#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define dbg(x) cerr << #x << " " << x << "\n"
const int N = 1e5;
int c[1 + N], f[1 + N], p[1 + N], h[1 + N], sz[1 + N];
vector <pair <int, int>> gr[1 + N];
int ft (int x) {
return (f[x] == x) ? x : f[x] = ft (f[x]);
}
void unite (int x, int y, int cost) {
x = ft (x); y = ft (y);
if (x != y) {
if (sz[x] > sz[y]) swap (x, y);
gr[x].pb ({y, cost});
gr[y].pb ({x, cost});
f[y] = x;
sz[x] += sz[y];
}
}
void dfs (int node, int par) {
p[node] = par;
h[node] = h[par] + 1;
for (pair <int, int> edge : gr[node])
if (edge.first != par) {
dfs (edge.first, node);
c[edge.first] = edge.second;
}
}
int main () {
ios::sync_with_stdio (false);
cin.tie (0); cout.tie (0);
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i <= n; i++)
f[i] = i, sz[i] = 1;
for (int i = m; i > 0; i--)
for (int j = i; j <= n; j += i)
unite (j, i, m -i + 1);
dfs (1, 0);
while (q--) {
int x, y;
cin >> x >> y;
int ans = 0;
while (x != y) {
if (h[x] > h[y]) {
ans = max (ans, c[x]);
x = p[x];
}
else {
ans = max (ans, c[y]);
y = p[y];
}
}
cout << ans << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
2816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
3064 KB |
Output is correct |
2 |
Correct |
53 ms |
2936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
3192 KB |
Output is correct |
2 |
Correct |
51 ms |
3192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
4344 KB |
Output is correct |
2 |
Correct |
87 ms |
4216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
4856 KB |
Output is correct |
2 |
Correct |
900 ms |
5992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
197 ms |
5880 KB |
Output is correct |
2 |
Correct |
150 ms |
5880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1596 ms |
9208 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
408 ms |
7928 KB |
Output is correct |
2 |
Correct |
576 ms |
8440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
565 ms |
9372 KB |
Output is correct |
2 |
Correct |
665 ms |
9208 KB |
Output is correct |