이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <cmath>
#include <unordered_map>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
typedef long long ll;
ll linf = 1e15+1;
inline void scoobydoobydoo(){
ios::sync_with_stdio(false);
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
}
const int MAXN = 1e6+7;
bool isVisited[MAXN];
vector<pair<int, int>> adj[MAXN];
bool visited[MAXN];
int subSz[MAXN];
int ans = 1e9+1;
int cnt[MAXN];
int subTreeSz(int node, int parent){
subSz[node] = 1;
for (auto& e : adj[node]){
if (e.first != parent && !isVisited[e.first])subSz[node] += subTreeSz(e.first, node);
}
return subSz[node];
}
int centroidFind(int node, int parent, int target){
for (auto& e : adj[node]){
if (e.first != parent && !isVisited[e.first] && target <= subSz[e.first]){
return centroidFind(e.first, node, target);
}
}
return node;
}
void cleaning(int node, int parent, int w){
cnt[w] = 1e9;
//cout << node << " " << parent << " " << w << endl;
for (auto& e : adj[node]){
if (e.first != parent && !isVisited[e.first]){
cleaning(e.first, node, w+e.second);
}
}
}
void bfs(int node, int parent, int k, int w, bool filling, int depth = 1){
if (w > k)return;
if (filling)(cnt[w] = min(cnt[w], depth));
else {
ans = min(ans, cnt[k-w]+depth);
}
//cout << node << " " << parent << " " << k << " " << w << " " << filling << " " << depth << endl;
for (auto& e : adj[node]){
if (e.first != parent && !isVisited[e.first]){
bfs(e.first, node, k, w + e.second, filling, depth+1);
}
}
}
void centroidDecomp(int node, int k){
node = centroidFind(node, -1, subTreeSz(node, -1)/2);
//cout << "CENTROID: " << node << endl;
isVisited[node] = true;
cnt[0] = 0;
for (auto& e : adj[node]){
if (isVisited[e.first])continue;
bfs(e.first, node, k, e.second, 0);
bfs(e.first, node, k, e.second, 1);
}
//cout << "CLEANING" << endl;
cleaning(node, node, 0);
//cout << ans << endl;
for (auto& e : adj[node]){
if (!isVisited[e.first])centroidDecomp(e.first, k);
}
}
int best_path(int N, int K, int H[][2], int L[]){
//cout << "no!" << endl;
for (int i = 0; i <= K; i++){
cnt[i] = 1e9;
}
//cout << "yo!" << endl;
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]});
}
centroidDecomp(0, K);
return (ans < 1e8 ? ans : -1);
}
# | 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... |