이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
#define pb push_back
#define f first
#define s second
#define pi pair<int,int>
vector<pi>ad[200000];
map<int,int>m[200000];
int ans = (int)1e9,x;
void dfs(int node,int parent,int depth,int cost){
long long t = cost * 2 + x;
m[node][cost] = depth;
for (auto i : ad[node])
if(i.first != parent){
dfs(i.first,node,depth + 1,cost + i.second);
if((int)m[i.first].size() > (int)m[node].size()) m[node].swap(m[i.first]);
for (auto v : m[i.first])
if(m[node].count(t - i.second))
ans = min(ans,m[node][t - v.first] + v.second - 2 * depth);
for (auto v : m[i.first])
if(m[node].count(v.first)) m[node][v.first] = min(m[node][v.first],v.second);
else m[node][v.first] = v.second;
}
}
int best_path(int N, int K, int H[][2], int L[]){
x = K;
for (int i = 0; i < N - 1;++i){
ad[H[i][0]].push_back({H[i][1],L[i]});
ad[H[i][1]].push_back({H[i][0],L[i]});
}
dfs(1,0,0,0);
return ans == (int)1e9 ? -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... |