Submission #671309

#TimeUsernameProblemLanguageResultExecution timeMemory
671309birthdaycakeRace (IOI11_race)C++17
0 / 100
110 ms262144 KiB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;

vector<pair<int,int>>adj[200001];
int dsu[200001],sz[200001];
map<int,int> store[200001];
int vs[200001],sum[200001];
int ans = INT_MAX, K;

int Find(int x){
    if(dsu[x] == x) return x;
    else return dsu[x] = Find(x);
}

void Unite(int a, int b, int cur_k, int cur_depth){
    a = Find(a); b = Find(b);
    if(a > b) swap(a,b);
    sz[b] += sz[a];
    dsu[a] = b;
    for(auto s: store[a]){
        if(store[b][cur_k - s.first] != 0){
            ans = min(ans, s.second - cur_k + store[b][cur_k - s.first] - cur_k);
        }
        if(store[b][s.first] == 0 or store[b][s.first] > s.second){
            store[b][s.first] = s.second;
        }
    }
    if(store[b][cur_k] != 0) ans = min(ans, store[b][cur_k] - cur_depth);
}
void dfs(int x, int par, int depth){
    vs[x] = 1;
    int vv = 0;
    for(auto s: adj[x]){
        if(!vs[s.first]){
            sum[s.first] += s.second;
            sum[s.first] += sum[x];
            dfs(s.first, x, depth + 1);
            vv++;
        }
    }
    if(vv == 0){
        map<int,int>p;
        p[sum[x]] = depth;
        store[x] = p;
    }else{
        for(auto s: adj[x]){
            if(s.first != par){
                Unite(x, s.first, 2 * sum[x] + K, depth);
            }
        }
    }
}
int best_path(int n, int k, int h[][2], int l[]){
    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]});
    }
    K = k;
    for(int i = 1; i <= n; i++){
        dsu[i] = i;
        sz[i] = 1;
    }
    dfs(1, 0, 1);
  return ans;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...