이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include"race.h"
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define vl vector<ll>
#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()
#define pb push_back
#define f first
#define s second
using namespace std;
const int sz = 2e5+5;
vector<pii>g[sz];
int vis[sz], lev[sz], p[30][sz], dis[sz];
void dfs(int node, int par)
{
for(auto [u, w] : g[node])
{
if(u == par)
continue;
p[0][u] = node;
lev[u] = lev[node] + 1;
dis[u] = dis[node] + w;
dfs(u, node);
}
}
int jump(int x, int k)
{
for(int i = 0; i < 26; i++)
{
if((k >> i) & 1)
x = p[i][x];
}
return x;
}
int lca(int x, int y)
{
if(lev[x] > lev[y])
swap(x, y);
int d = lev[y] - lev[x];
y = jump(y, d);
if(x == y)
return x;
else
{
for(int i = 25; i >= 0; i--)
{
if(p[i][x] != p[i][y])
{
x = p[i][x];
y = p[i][y];
}
}
return p[0][x];
}
}
int best_path(int N, int K, int H[][2], int L[])
{
int n, k, i, j, x, y, z;
n = N;
k = K;
for(i = 0; i < n - 1; i++)
{
x = H[i][0];
y = H[i][1];
z = L[i];
x++;
y++;
g[x].pb({y, z});
g[y].pb({x, z});
}
dfs(1, 1);
for(i = 1; i < 26; i++)
{
for(j = 1; j <= n; j++)
p[i][j] = p[i-1][p[i-1][j]];
}
int ans = INT_MAX;
for(i = 1; i <= n; i++)
{
for(j = i + 1; j <= n; j++)
{
ll a = lca(i, j);
ll ds = dis[i] + dis[j] - 2 * dis[a];
if(ds == k)
ans = min(ans, lev[i] + lev[j] - 2 * lev[a]);
}
}
int f = -1;
if(ans == LLONG_MAX)
return f;
else
return ans;
}
# | 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... |