이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
vector<pair<int,int>>adj[200001];
int dsu[200001],sz[200001];
map<long long,int> store[200001];
long long sum[200001];
long long ans = INT_MAX, K;
int Find(int x){
return x == dsu[x] ? x : dsu[x] = Find(dsu[x]);
}
void Unite(int a, int b, long long cur_k, int cur_depth){
a = Find(a); b = Find(b);
if(sz[a] > sz[b]) swap(a,b);
sz[b] += sz[a];
dsu[a] = b;
for(auto &s: store[a]){
if(store[b].count(cur_k - s.first)){
ans = min(ans, (long long)s.second - cur_depth + store[b][cur_k - s.first] - cur_depth);
}
}
for(auto &s: store[a]){
if(!store[b].count(s.first) or store[b][s.first] > s.second){
store[b][s.first] = s.second;
}
}
}
void dfs(int x, int par, int depth){
for(auto s: adj[x]){
if(s.first != par){
sum[s.first] = s.second + sum[x];
dfs(s.first, x, depth + 1);
}
}
store[x][sum[x]] = depth;
for(auto s: adj[x]){
if(s.first != par){
Unite(x, s.first, 2ll * sum[x] + K, depth);
}
}
// int b = Find(x);
// if(store[b].count(sum[x] + K) != 0) ans = min(ans, (long long)store[b][sum[x] + K] - depth);
}
signed best_path(signed n, signed k, signed h[][2], signed l[]){
for (int i = 1 ; i <= n ; i++) sz[i] = 1, dsu[i] = i;
ans = INT_MAX;
for(int i = 0; i < n - 1; i++){
h[i][0]++; h[i][1]++;
adj[h[i][0]].push_back({h[i][1], l[i]});
adj[h[i][1]].push_back({h[i][0], l[i]});
}
K = k;
dfs(1, 0, 1);
if(ans == INT_MAX) ans = -1;
return 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... |