이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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], 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 x:G[node])
if (x.first != p && !vis[x.first])
cnt(x.first, node, yes, sum + x.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... |