This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "race.h"
using namespace std;
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair<int,int> pii;
typedef vector<long long> vl;
typedef vector<int> vi;
const int N = 1e6 + 5;
vector<pii> G[N];
int ans = 1e9, arr[N], tmp[N], sz[N], l[N], r[N], k, mx,n;
bool vis[N];
int get_sz(int node, int p = -1){
sz[node] = 1;
for(auto next : G[node]){
if(next.first != p && !vis[next.first]){
sz[node] += get_sz(next.first, node);
}
}
return sz[node];
}
int get_centroid(int node, int size, int p = -1){
for(auto next : G[node]){
if(next.first == p || vis[next.first]) continue;
if(sz[next.first] >= size) return get_centroid(next.first, size, node);
}
return node;
}
void cnt(int node, int p, int yes, int sum, int level){
if(sum > k) return;
if(sum == k) ans = min(ans, level);
if(yes == 1) tmp[sum] = min(tmp[sum], level);
else if(yes) tmp[sum] = 1e9;
else ans = min(ans, level + tmp[k-sum]);
for(auto next : G[node]){
if(next.first != p && !vis[next.first])
cnt(next.first, node, yes, sum + next.second, level+1);
}
}
void decompose(int node = 0){
get_sz(node);
int c = get_centroid(node, sz[node]/2);
vis[c] = 1;
mx = 0;
for(auto next : G[node]){
if(!vis[next.first])
cnt(next.first, c, 0, next.second, 1), cnt(next.first, c, 1, next.second, 1);
}
for(auto next : G[node]){
if(!vis[next.first])
cnt(next.first, c, 2, next.second, 1);
}
for(auto next : G[node]){
if(!vis[next.first]) decompose(next.first);
}
}
int best_path(int N, int K, int H[][2], int L[]){
fill(tmp, tmp+1000000 + 5, 1e9);
n= N, k = K;
for(int i = 0; i < N-1; i++){
G[H[i][1]].pb({H[i][0], L[i]});
G[H[i][0]].pb({H[i][1], L[i]});
}
decompose();
return (ans == 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... |