Submission #1288505

#TimeUsernameProblemLanguageResultExecution timeMemory
1288505tisho0Race (IOI11_race)C++20
0 / 100
6 ms9784 KiB
#include <bits/stdc++.h>
#include "race.h"
#define endl '\n'

using namespace std;

const int MAXN = 2e5;
int n, k, depth[MAXN + 5], ans;
long long sum[MAXN + 5];
map<long long,int>m[MAXN + 5];
vector<pair<int,int>>v[MAXN + 5];

void _merge(int x, int y)
{
    if(m[x].size() < m[y].size())swap(m[x], m[y]);


    for(auto [key, value]: m[y])
    {
        if(m[x].count(k - key + 2 * sum[x]))
        {
            ans = min(ans, m[x][k - key + 2 * sum[x]] + value - 2 * depth[x]);
        }
    }

    for(auto [key, value]: m[y])
    {
        if(m[x][key] == 0)
        {
            m[x][key] = value;
        }
        else m[x][key] = min(m[x][key], value);
    }
}

void dfsp(int node, int parent)
{
    depth[node] = depth[parent] + 1;
    for(auto i: v[node])
    {
        if(i.first == parent)continue;
        sum[i.first] = sum[node] + i.second;
        dfsp(i.first, node);
    }
}

void dfs(int node, int parent)
{
    for(auto i: v[node])
    {
        if(i.first == parent)continue;
        dfs(i.first, node);
        _merge(i.first, node);
    }
}

int best_path(int N, int K, int H[][2], int L[])
{
    n = N; k = K; ans = 1e9;
    for(int i = 0; i < n - 1; i++)
    {
        v[H[i][0]].push_back({H[i][1], L[i]});
        v[H[i][1]].push_back({H[i][0], L[i]});
    }
    depth[0] = -1;
    dfsp(0, 0);

    for(int i = 0; i < n; i++){
        m[i][sum[i]] = depth[i];
    }

    dfs(0, 0);
    if(ans == 1e9)ans = -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...