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;
const int MAXN = 100005;
int n, m, q;
int a[MAXN], b[MAXN], siz[MAXN], par[MAXN];
int low[MAXN], high[MAXN];
vector <int> v[MAXN];
void precompute () {
for (int i=1; i<=q; i++) {
low[i] = 1;
high[i] = m;
}
}
void build () {
for (int i=1; i<=n; i++) {
par[i] = i;
siz[i] = 1;
}
}
int nadi (int x) {
if (x == par[x]) return x;
par[x] = nadi(par[x]);
return par[x];
}
void spoji (int x, int y) {
x = nadi(x);
y = nadi(y);
if (x == y) return;
if (siz[x] < siz[y]) swap(x, y);
par[y] = x;
if (siz[x] == siz[y]) siz[x]++;
}
bool bs () {
for (int i=1; i<=m; i++) {
v[i].clear();
}
for (int i=1; i<=q; i++) {
v[(low[i] + high[i] + 1)/2].push_back(i);
}
build();
bool ok = 1;
for (int i=m; i>=1; i--) {
for (int j=2*i; j<=n; j+=i) {
spoji(i, j);
}
for (int j=0; j<v[i].size(); j++) {
int ind = v[i] [j];
//cout << "bla " << i << " " << a[ind] << " " << b[ind] << endl;
if (nadi(a[ind]) == nadi(b[ind])) {
low[ind] = i;
} else {
high[ind] = i-1;
}
if (low[ind] != high[ind]) ok = 0;
}
}
return ok;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> q;
precompute();
for (int i=1; i<=q; i++) {
cin >> a[i] >> b[i];
}
while (!bs());
for (int i=1; i<=q; i++) {
cout << m - low[i] + 1 << endl;
}
return 0;
}
Compilation message (stderr)
pictionary.cpp: In function 'bool bs()':
pictionary.cpp:54:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j=0; j<v[i].size(); j++) {
~^~~~~~~~~~~~
# | 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... |