Submission #881362

#TimeUsernameProblemLanguageResultExecution timeMemory
881362Karoot경주 (Race) (IOI11_race)C++17
0 / 100
17 ms33884 KiB
#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;
}
 
int vals[MAXN];
int counter = 0;
 
void cleaning(int node, int parent, int w){
    cnt[w] = 1e9;
    for (auto& e : adj[node]){
        if (e.first != parent && !isVisited[e.first]){
            cleaning(e.first, node, w+vals[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 if (cnt[k-w]>=0){
        //cout << node << " " << parent << " " << w << " " << depth << endl;
        //cout << cnt[k-w] << endl;
        ans = min(ans, cnt[k-w]+depth);
    }
    for (auto& e : adj[node]){
        if (e.first != parent && !isVisited[e.first]){
            bfs(e.first, node, k, w + vals[e.second], filling, depth+1);
        }
    }
}
 
 
void centroidDecomp(int node, int k){
    node = centroidFind(node, -1, subTreeSz(node, -1)/2);
    isVisited[node] = true;
    //cout << "CENTROID: " << node << endl;
    cleaning(node, node, 0);
    cnt[0] = 0;
    for (auto& e : adj[node]){
        bfs(e.first, node, k, vals[e.second], 0);
        bfs(e.first, node, k, vals[e.second], 1);
    }
 
    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[]){
    for (int i = 0; i <= K; i++){
        cnt[i] = 1e9;
    }
    for (int i = 0; i < N-1; i++){
        vals[i] = L[i];
        adj[H[i][0]].push_back({H[i][1], i});
        adj[H[i][1]].push_back({H[i][0], i});
    }
    centroidDecomp(0, K);
    return (ans < 1e8 ? ans : -1);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...