#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
#define f first
#define s second
#define pii pair<int, int>
bool ckmin(int& a, int b){ return b < a ? a = b, true : false; }
bool ckmax(int& a, int b){ return b > a ? a = b, true : false; }
struct Highway{
int a, b, w;
};
const int N = 2e5 + 5;
int n, k, ans = 1e9;
ll c[N];
int d[N];
Highway h[N];
map<int, int> mp[N];
vector<pii> adj[N];
void dfs(int node, int prev, ll sum = 0, int dist = 0){
c[node] = sum;
d[node] = dist;
mp[node][sum] = dist;
for(auto x : adj[node]){
if(x.f == prev) continue;
dfs(x.f, node, sum + x.s, dist + 1);
}
}
void dfs2(int node, int prev){
ll t = k + 2 * c[node];
for(auto x : adj[node]){
if(x.f == prev) continue;
dfs2(x.f, node);
if(sz(mp[x.f]) > sz(mp[node])) {
swap(mp[x.f], mp[node]);
}
for(auto y : mp[x.f]){
if(mp[node].count(t - y.f)){
ckmin(ans, mp[node][t - y.f] + y.s - 2 * d[node]);
}
}
for(auto y : mp[x.f]){
if(!mp[node].count(y.f)){
mp[node][y.f] = y.s;
}
else{
ckmin(mp[node][y.f], y.s);
}
}
}
}
int best_path(int n, int k, int edges[][2], int weights[]) {
if (k == 1) { return 0; }
for (int i = 0; i < n - 1; i++) {
int u = edges[i][0];
int v = edges[i][1];
adj[u].pb({v, weights[i]});
adj[v].pb({u, weights[i]});
}
dfs(0, -1);
return (ans == 1e9 ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
14424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
14424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
14424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
14424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |