이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
typedef pair<int, int> pii;
int ini[maxn], fim[maxn], n, m, q;
int pai[maxn], sz[maxn];
pii query[maxn];
vector<int> v[maxn];
int Find(int x)
{
if (pai[x] == x) return x;
return pai[x] = Find(pai[x]);
}
void Join(int x, int y)
{
x = Find(x), y = Find(y);
if (x == y) return;
if (sz[x] < sz[y]) swap(x, y);
sz[x] += sz[y], pai[y] = x;
}
void init(void)
{
for (int i = 1; i <= m; i++)
v[i].clear();
for (int i = 1; i <= n; i++)
pai[i] = i, sz[i] = 1;
}
void solve(void)
{
bool flag = 1;
while (flag)
{
init();
flag = 0;
for (int i = 1; i <= q; i++)
{
if (ini[i] != fim[i])
{
int mid = (ini[i]+fim[i])/2;
v[mid].push_back(i), flag = 1;
}
}
for (int i = 1; i <= m; i++)
{
int d = m-i+1;
for (int j = d; j <= n; j += d)
Join(d, j);
for (auto x: v[i])
{
if (Find(query[x].first) == Find(query[x].second)) fim[x] = i;
else ini[x] = i+1;
}
}
}
}
int main(void)
{
cin >> n >> m >> q;
for (int i = 1; i <= q; i++)
{
cin >> query[i].first >> query[i].second;
ini[i] = 1, fim[i] = m;
}
solve();
for (int i = 1; i <= q; i++)
cout << fim[i] << "\n";
}
# | 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... |