이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
# include <bits/stdc++.h>
# include "race.h"
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define deb(x) cerr << #x << " = " << x << endl;
# define pll pair <ll, ll>
# define pii pair <int, int>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll maxn = 2e5 + 25;
const ll inf = 2e9 + 0;
const ll mod = 998244353;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
using namespace std;
int n, k, ans;
vector <pii> g[maxn];
int sz[maxn];
bool used[maxn];
map <ll, int> mn;
void dfs_calc (int v, int pa)
{
sz[v] = 1;
for (pii i : g[v])
{
int to = i.ff, w = i.ss;
if (to == pa || used[to]) continue;
dfs_calc(to, v);
sz[v] += sz[to];
}
}
int find_c (int v, int pa, int siz)
{
for (pii i : g[v])
{
int to = i.ff, w = i.ss;
if (to == pa || used[to]) continue;
if (sz[to] + sz[to] > siz)
{
return find_c(to, v, siz);
}
}
return v;
}
void dfs_add (int v, int pa, int d1, int d2)
{
mn[d1] = min(mn[d1], d2);
for (pii i : g[v])
{
int to = i.ff, w = i.ss;
if (to == pa || used[to]) continue;
dfs_add(to, v, d1 + w, d2 + 1);
}
}
void dfs_del (int v, int pa, ll d1, int d2)
{
mn[d1] = inf;
for (pii i : g[v])
{
int to = i.ff, w = i.ss;
if (to == pa || used[to]) continue;
dfs_del(to, v, d1 + w, d2 + 1);
}
}
void dfs_ans (int v, int pa, ll d1, int d2)
{
if (d1 > k) return;
ans = min(ans, d2 + mn[k - d1]);
for (pii i : g[v])
{
int to = i.ff, w = i.ss;
if (to == pa || used[to]) continue;
dfs_ans(to, v, d1 + w, d2 + 1);
}
}
void centroid (int v)
{
dfs_calc(v, v);
v = find_c(v, v, sz[v]);
for (pii i : g[v])
{
int to = i.ff, w = i.ss;
if (used[to]) continue;
dfs_ans(to, v, w, 1);
dfs_add(to, v, w, 1);
}
for (pii i : g[v])
{
int to = i.ff, w = i.ss;
if (used[to]) continue;
dfs_del(to, v, w, 1);
}
used[v] = 1;
for (pii i : g[v])
{
int to = i.ff, w = i.ss;
if (used[to]) continue;
centroid(to);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N, k = K, ans = inf;
for(int i = 0; i < n - 1; ++i){
g[H[i][0] + 1].pb({H[i][1] + 1, L[i]});
g[H[i][1] + 1].pb({H[i][0] + 1, L[i]});
}
for (int i = 1; i <= k; ++i) mn[i] = inf;
centroid(1);
return (ans == inf ? -1 : ans);
}
컴파일 시 표준 에러 (stderr) 메시지
race.cpp: In function 'void dfs_calc(int, int)':
race.cpp:36:18: warning: unused variable 'w' [-Wunused-variable]
36 | int to = i.ff, w = i.ss;
| ^
race.cpp: In function 'int find_c(int, int, int)':
race.cpp:47:18: warning: unused variable 'w' [-Wunused-variable]
47 | int to = i.ff, w = i.ss;
| ^
race.cpp: In function 'void centroid(int)':
race.cpp:111:18: warning: unused variable 'w' [-Wunused-variable]
111 | int to = i.ff, w = i.ss;
| ^
# | 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... |