이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 2e5 + 7;
const ll oo = 1e9 + 69;
int n, k, w[maxn], u, v, ed[maxn][2], dep[maxn], ans;
ll f[maxn];
vector<pa> g[maxn];
unordered_map<ll, int> mp[maxn];
void dfs(int u, int par) {
for(auto e:g[u]) {
int v = e.fi, w = e.se;
if(v == par) continue;
f[v] = f[u] + w;
dep[v] = dep[u] + 1;
dfs(v, u);
}
}
void dfs2(int u, int par) {
mp[u][f[u]] = dep[u];
if(f[u] == k) mini(ans, dep[u]);
for(auto e:g[u]) {
int v = e.fi;
if(v == par) continue;
dfs2(v, u);
if(Size(mp[u]) < Size(mp[v])) {
swap(mp[u], mp[v]);
}
for(auto e:mp[v]) {
if(!mp[u].count(e.fi)) mp[u][e.fi] = e.se;
else mini(mp[u][e.fi], e.se);
ll rem = k + 2LL * f[u] - e.fi;
if(mp[u].count(rem)) mini(ans, mp[u][e.fi] + mp[u][rem] - 2 * dep[u]);
}
}
}
int best_path(int n, int len, int ed[][2], int w[]) {
// cin>>n>>k;
// for(int i = 0; i < n - 1; i++) {
// cin>>ed[i][0]>>ed[i][1];
// }
k = len;
for(int i = 0; i < n - 1; i++) {
// cin>>w[i];
u = ed[i][0] + 1, v = ed[i][1] + 1;
g[u].pb({v, w[i]});
g[v].pb({u, w[i]});
}
ans = oo;
dfs(1, 0);
dfs2(1, 0);
return (ans == oo ? -1: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... |