#include "race.h"
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using pii = pair<int, int>;
const int MAXN = 2e5 + 5;
const int oo32 = 2e9;
#define sz(a) (int)a.size()
int N, K;
vector<pii> adj[MAXN];
map<i64, int> dp[MAXN];
int ans = oo32;
void DFS(int u, i64 c = 0, int depth = 0, int p = -1) {
dp[u][c] = depth;
for(auto [v, w] : adj[u]) {
if(v == p) continue;
DFS(v, c + w, depth + 1, u);
if(dp[u].size() < dp[v].size()) swap(dp[u], dp[v]);
for(auto [x, y] : dp[v]) {
if(dp[u].find(c + K - (x - c)) != dp[u].end())
ans = min(ans, y + dp[u][c + K - (x - c)] - 2 * depth);
}
for(auto [x, y] : dp[v]) {
if(dp[u].find(x) != dp[u].end())
dp[u][x] = min(dp[u][x], y);
else dp[u][x] = y;
}
}
}
int best_path(int _N, int _K, int H[][2], int L[]) {
N = _N;
K = _K;
ans = oo32;
for(int i = 0; i < N; i++) {
int u = H[i][0];
int v = H[i][1];
int w = L[i];
u++;
v++;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
DFS(1);
return (ans == oo32 ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
18012 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
18012 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
18012 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
18012 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |