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;
int p[200005][20], depth[200005], par[200005];
vector<int> child[200005];
int findPar(int u)
{
return (par[u]==u)?u:(par[u]=findPar(par[u]));
}
void dfs(int u)
{
for (int v:child[u])
{
depth[v]=depth[u]+1;
dfs(v);
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin >> n >> m >> q;
for (int i=1; i<=n+m; i++)
par[i]=i;
for (int i=1; i<=m; i++)
{
for (int u=m-i+1; u<=n; u+=m-i+1)
{
int v=findPar(u);
if (v<n+i)
{
p[v][0]=par[v]=n+i;
child[n+i].push_back(v);
}
}
}
for (int i=1; i<20; i++)
for (int j=1; j<=n+m; j++)
p[j][i]=p[p[j][i-1]][i-1];
dfs(n+m);
for (int i=1; i<=q; i++)
{
int u, v;
cin >> u >> v;
if (depth[u]<depth[v])
swap(u, v);
int x=depth[u]-depth[v];
for (int j=0; j<20; j++)
if (x&(1<<j))
u=p[u][j];
if (u==v)
{
cout << u-n << '\n';
continue;
}
for (int j=19; j>=0; j--)
{
if (p[u][j]!=p[v][j])
{
u=p[u][j];
v=p[v][j];
}
}
cout << p[u][0]-n << '\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... |