#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int maxn = 1e5 + 5;
struct DSU {
int n, par[maxn], size[maxn];
void config(int _n) {
n = _n + 1;
for(int i=1; i<=n; i++) par[i] = i, size[i] = 1;
}
int get(int u) {
if(u == par[u]) return u;
return par[u] = get(par[u]);
}
bool uni(int u, int v) {
u = get(u), v = get(v);
if(u == v) return false;
if(size[u] < size[v]) swap(u, v);
size[u] += size[v];
par[v] = u;
return true;
}
bool sameSet(int &u, int &v) { return get(u) == get(v); }
void reset() {
for(int i=1; i<=n; i++) par[i] = i, size[i] = 1;
}
};
int32_t main() {
int n, m, q;
cin >> n >> m >> q;
vector<pii> qus(q);
for(pii &x : qus) cin >> x.first >> x.second;
vector<int> L(q, 1), R(q, m), ANS(q, m);
bool changed = 1;
stack<int> to_check[m+1];
DSU dsu; dsu.config(n);
while(changed) {
changed = 0;
for(int i=0; i<q; i++) {
if(L[i] <= R[i]) {
to_check[(L[i] + R[i]) / 2].push(i);
changed = 1;
}
}
int curr = m;
dsu.reset();
for(int i=1; i<=m; i++) {
for(int j=curr; j<=n; j+=curr)
if(j+curr <= n) dsu.uni(j, j+curr);
while(!to_check[i].empty()) {
int id = to_check[i].top();
to_check[i].pop();
if(dsu.sameSet(qus[id].first, qus[id].second)) ANS[id] = i, R[id] = i - 1;
else L[id] = i + 1;
}
curr--;
}
}
for(int &x : ANS) cout << x << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
3540 KB |
Output is correct |
2 |
Correct |
37 ms |
3152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
4180 KB |
Output is correct |
2 |
Correct |
46 ms |
3668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
17488 KB |
Output is correct |
2 |
Correct |
49 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
15388 KB |
Output is correct |
2 |
Correct |
46 ms |
3404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
33716 KB |
Output is correct |
2 |
Correct |
70 ms |
15904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
3664 KB |
Output is correct |
2 |
Correct |
123 ms |
24440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
147 ms |
41296 KB |
Output is correct |
2 |
Correct |
132 ms |
18772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
76 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |