이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <unistd.h>
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#ifndef Local
#pragma GCC optimize("O3,unroll-loops")
#endif
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpi vector<pii>
#define vvpi vector<vpi>
#define vb vector<bool>
#define vvb vector<vb>
#define endl '\n'
#define sp << " " <<
#define F(i, s, n) for(int i = s; i < n; i++)
#define pb push_back
#define fi first
#define se second
#include "race.h"
int mod = 1e9 + 7;
int inf = 1e18;
const int N = 2e5+5;
vpi adj[N];
map<int, int> mp[N];
int k;
int32_t h[N][2];
int32_t l[N];
int ans = inf;
inline void dfs(int node, int par, int depth, int depl) {
for(pii go : adj[node]) if(go.fi != par) dfs(go.fi, node, depth+1, depl+go.se);
mp[node][depl] = depth+1;
for(pii go : adj[node]) if(go.fi != par) {
if(mp[go.fi].size() > mp[node].size()) swap(mp[go.fi], mp[node]);
for(pii el : mp[go.fi]) {
int remK = k - (el.fi - depl);
if(mp[node][remK + depl]) {
ans = min(ans, mp[node][remK + depl] + el.se - 2 - depth*2);
}
}
for(pii el : mp[go.fi]) {
if(mp[node][el.fi]) mp[node][el.fi] = min(mp[node][el.fi], el.se);
else mp[node][el.fi] = el.se;
}
mp[go.fi].clear();
}
}
int32_t best_path(int32_t n, int32_t k, int32_t h[][2], int32_t l[]) {
F(i, 0, n-1) {
int u = h[i][0], v = h[i][1];
adj[u].pb({v, l[i]});
adj[v].pb({u, l[i]});
}
::k = k;
ans = inf;
dfs(0, -1, 0, 0);
if(ans < 1e16) return ans;
else return -1;
}
/*
void solve() {
int32_t n, k;
cin >> n >> k;
F(i, 0, n-1) cin >> h[i][0] >> h[i][1];
F(i, 0, n-1) cin >> l[i];
cout << best_path(n, k, h, l) << endl;
}
void setIO() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef Local
freopen("local.in", "r", stdin);
freopen("local.out", "w", stdout);
#else
// freopen("disrupt.in","r",stdin);
// freopen("disrupt.out","w",stdout);
#endif
}
signed main() {
setIO();
int t = 1;
//cin >> t;
while(t--) solve();
}*/
# | 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... |