10242022-02-25 07:33:49Szin AttilaTársaság (50)cpp14Wrong answer 34/5012ms4008 KiB
#include <bits/stdc++.h>
using namespace std;
#define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using ll = long long;

const ll maxN = 2e5 + 5;
const ll MOD = 1e9 + 7;

ll n, k;
vector<vector<pair<ll, ll> > > g;
vector<ll> val, b;
ll a;

void dfs1(ll x)
{
    for(pair<ll, ll> sz : g[x]) {
        val[sz.first] = val[x] + sz.second;
        dfs1(sz.first);
    }
}

ll dfs(ll x, ll mid) {
    ll maxi = 0;

    for(pair<ll, ll> sz : g[x]) {
        ll temp = dfs(sz.first, mid);
        maxi = max(maxi, temp);
    }

    maxi += b[x];

    if(maxi > mid) {
        a++;
        maxi = 0;
    }

    return maxi;
}

int main() {
   InTheNameOfGod;
    
    cin >> n >> k;

    g.resize(n+1);
    val.resize(n+1, 0);
    b.resize(n+1, 0);

    for(ll i = 2; i <= n; i++) {
        ll x,y;
        cin >> x >> y;
        g[x].push_back({i, y});
        b[i] = y;
    }

    dfs1(1);

    ll maxi = 0;
    for(ll i = 1; i <= n; i++) maxi = max(maxi, val[i]);

    ll l = 0, r = maxi, mid, mo;
    while(l < r) {
        mid = (l + r) / 2;
        val.assign(n+1, 0);
        a = 0;
        dfs(1, mid);

        if(a > k) {
            l = mid + 1;         
        }
        else {
            r = mid - 1;
            mo = mid;
        }
    }
    cout << mo;

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base34/50
1Accepted0/02ms1888 KiB
2Wrong answer0/07ms2580 KiB
3Accepted3/31ms1992 KiB
4Accepted3/31ms2004 KiB
5Wrong answer0/31ms2008 KiB
6Wrong answer0/31ms2012 KiB
7Wrong answer0/31ms2044 KiB
8Accepted3/32ms2076 KiB
9Accepted3/33ms2292 KiB
10Accepted3/33ms2380 KiB
11Wrong answer0/36ms2608 KiB
12Accepted3/38ms2836 KiB
13Accepted4/47ms2908 KiB
14Wrong answer0/48ms3172 KiB
15Accepted4/49ms3376 KiB
16Accepted4/410ms3600 KiB
17Accepted4/412ms4008 KiB