This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**Lucky Boy**/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define maxc 1000000007
#define maxn 100005
#define maxm 500005
#define pii pair <int,int>
#define Task "Pictionary"
template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar())) x=x*10+c-48;if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10);putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');}
template <typename T> inline void writeln(T x){write(x);putchar('\n');}
using namespace std;
int n,m,q,root[maxn],cnt,h[maxn];
pii par[maxn][17];
vector <pii> a[maxn];
int Get_root(int x)
{
return root[x] ? root[x] = Get_root(root[x]) : x;
}
void Prepare()
{
FOR(i,0,m-1)
{
int cur = m - i;
for (int j = 2 * cur; j <= n; j += cur)
{
int u = Get_root(cur),
v = Get_root(j);
if (u == v) continue;
root[u] = v;
a[u].pb(mp(v,i));
a[v].pb(mp(u,i));
++cnt;
if (cnt == n - 1) break;
}
if (cnt == n - 1) break;
}
}
void Dfs(int u)
{
for (auto v : a[u])
if (par[u][0].F != v.F)
{
h[v.F] = h[u] + 1;
par[v.F][0] = mp(u,v.S);
FOR(i,1,16)
{
par[v.F][i].F = par[par[v.F][i-1].F][i-1].F;
par[v.F][i].S = max(par[v.F][i-1].S,par[par[v.F][i-1].F][i-1].S);
}
Dfs(v.F);
}
}
int Lca(int u,int v)
{
int res = -maxc;
if (h[u] < h[v]) swap(u,v);
int dis = h[u] - h[v];
FOR(i,0,16)
if ((dis >> i) & 1)
{
res = max(res,par[u][i].S);
u = par[u][i].F;
}
if (u == v) return res + 1;
FORD(i,16,0)
if (par[u][i].F != par[v][i].F)
{
res = max(res,par[u][i].S);
res = max(res,par[v][i].S);
u = par[u][i].F;
v = par[v][i].F;
}
res = max(res,par[u][0].S);
res = max(res,par[v][0].S);
return res + 1;
}
int main()
{
//ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//freopen(Task".inp", "r",stdin);
//freopen(Task".out", "w",stdout);
read(n),read(m),read(q);
Prepare();
Dfs(1);
while (q--)
{
int x,y;
read(x),read(y);
writeln(Lca(x,y));
}
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... |