이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define lwb lower_bound
#define upb upper_bound
#define TASKNAME "NAME"
void init()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
///freopen(TASKNAME".INP","r",stdin); freopen(TASKNAME".OUT","w",stdout);
}
const int SZ = 1e6+5;
const ll INF = INT_MAX / 2, MOD = 1e9+7, INFLL = 2e18;
const double epsilon = 1e-3;
int n,m,q;
struct DSU
{
int s[SZ], par[SZ];
void init(int n)
{
for(int i = 1; i <= n; i++)
{
s[i] = 1;
par[i] = i;
}
}
int get(int u)
{
return par[u] == u ? u : par[u] = get(par[u]);
}
bool join(int u, int v)
{
u = get(u);
v = get(v);
if(u == v) return false;
if(s[u] < s[v]) swap(u, v);
s[u] += s[v];
par[v] = u;
return true;
}
} dsu;
vector<pii> mst[SZ];
int h[SZ], up[SZ][20], val[SZ][20];
void dfs(int u, int pre)
{
for(pii e : mst[u])
{
int v = e.fi, w = e.se;
if(v == pre) continue;
h[v] = h[u] + 1;
val[v][0] = w;
up[v][0] = u;
for(int i = 1; i < 20; i++)
{
up[v][i] = up[up[v][i-1]][i-1];
val[v][i] = max(val[v][i-1], val[up[v][i-1]][i-1]);
}
dfs(v, u);
}
}
int res(int u, int v)
{
int mx = 0;
if(h[u] != h[v])
{
if(h[u] < h[v]) swap(u, v);
int diff = h[u] - h[v];
for(int i = 0; i < 20; i++)
{
if(diff >> i & 1)
{
mx = max(mx, val[u][i]);
u = up[u][i];
}
}
}
if(u == v) return mx;
for(int i = 19; i >= 0; i--)
{
if(up[u][i] != up[v][i])
{
mx = max({mx, val[u][i], val[v][i]});
u = up[u][i];
v = up[v][i];
}
}
return max({mx, val[u][0], val[v][0]});
}
int main()
{
init();
cin >> n >> m >> q;
dsu.init(n);
for(int i = m; i >= 1; i--)
{
for(int j = 2*i; j <= n; j += i)
{
int u = j-i, v = j;
if(dsu.join(u, v))
{
mst[u].pb({v, m - i + 1});
mst[v].pb({u, m - i + 1});
}
}
}
dfs(1, 0);
while(q--)
{
int u,v;
cin >> u >> v;
cout << res(u, v) << '\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... |