This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |