10232022-02-25 07:17:44Szin AttilaTársaság (50)cpp14Wrong answer 4/5012ms3600 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;

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 ret = 0;
    if(val[x] > mid) {
        val[x] = 0;
        ret++;
    }

    for(pair<ll, ll> sz : g[x]) {
        val[sz.first] = val[x] + sz.second;
        ret += dfs(sz.first, mid);
    }
    return ret;
}

int main() {
   InTheNameOfGod;

    
    
    cin >> n >> k;

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

    for(ll i = 2; i <= n; i++) {
        ll x,y;
        cin >> x >> y;
        g[x].push_back({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);

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

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base4/50
1Accepted0/02ms1824 KiB
2Wrong answer0/06ms2536 KiB
3Wrong answer0/31ms2008 KiB
4Wrong answer0/31ms2008 KiB
5Wrong answer0/31ms2008 KiB
6Wrong answer0/31ms2028 KiB
7Wrong answer0/31ms2044 KiB
8Wrong answer0/32ms2076 KiB
9Wrong answer0/32ms2276 KiB
10Wrong answer0/33ms2356 KiB
11Wrong answer0/34ms2572 KiB
12Wrong answer0/36ms2672 KiB
13Wrong answer0/48ms2916 KiB
14Wrong answer0/48ms3120 KiB
15Wrong answer0/48ms3220 KiB
16Accepted4/410ms3460 KiB
17Wrong answer0/412ms3600 KiB