이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "race.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 2e5 + 2;
vector<pii> adj[MAXN];
map<ll, int> m[MAXN];
map<ll, int> has[MAXN];
int n, k, ans = MAXN, sub[MAXN], inc_val[MAXN], cnt[MAXN];
ll inc_path[MAXN];
void dfs(int node, int anc){
sub[node] = 1;
for(pii x : adj[node]){
if(x.first == anc) continue;
dfs(x.first, node);
sub[node] += sub[x.first];
}
if(adj[node].size() == 1 && anc != -1){
m[node][0] = 0;
has[node][0] = 1;
}
for(pii x : adj[node]){
if(x.first == anc) continue;
++inc_val[x.first];
inc_path[x.first] += x.second;
}
m[node][0] = 0;
has[node][0] = 1;
cnt[node] = 1;
for(pii x : adj[node]){
if(x.first == anc) continue;
if(cnt[x.first] >= cnt[node]){
swap(m[node], m[x.first]);
swap(has[node], has[x.first]);
swap(inc_val[node], inc_val[x.first]);
swap(inc_path[node], inc_path[x.first]);
swap(cnt[node], cnt[x.first]);
}
for(pair<ll, int> p : m[x.first]){
ll len = p.first + inc_path[x.first];
int val = p.second + inc_val[x.first];
if(k >= len && has[node][k - len - inc_path[node]]){
ans = min(ans, val + m[node][k - len - inc_path[node]] + inc_val[node]);
}
}
for(pair<ll, int> p : m[x.first]){
ll len = p.first + inc_path[x.first] - inc_path[node];
int val = p.second + inc_val[x.first];
if(has[node][len]){
m[node][len] = min(m[node][len], val - inc_val[node]);
} else {
++cnt[node];
has[node][len] = 1;
m[node][len] = val - inc_val[node];
}
}
}
}
int best_path(int _n, int _k, int h[][2], int l[]){
n = _n; k = _k;
for(int i = 0; i < n - 1; ++i){
adj[h[i][0]].push_back({h[i][1], l[i]});
adj[h[i][1]].push_back({h[i][0], l[i]});
}
dfs(0, -1);
if(ans == MAXN) return -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... |