10222022-02-25 07:17:07Szin AttilaTársaság (50)cpp14Time limit exceeded 4/50495ms3344 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 int maxN = 2e5 + 5;
const int MOD = 1e9 + 7;

int n, k;
vector<vector<pair<int, int> > > g;
vector<int> val;

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

int dfs(int x, int mid) {
    int ret = 0;
    if(val[x] > mid) {
        val[x] = 0;
        ret++;
    }

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

    dfs1(1);

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

    int 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/02ms1884 KiB
2Time limit exceeded0/0409ms1248 KiB
3Wrong answer0/31ms2020 KiB
4Wrong answer0/31ms2028 KiB
5Wrong answer0/31ms2032 KiB
6Wrong answer0/31ms2040 KiB
7Wrong answer0/31ms2056 KiB
8Wrong answer0/32ms2096 KiB
9Wrong answer0/32ms2256 KiB
10Wrong answer0/33ms2352 KiB
11Wrong answer0/34ms2516 KiB
12Wrong answer0/36ms2616 KiB
13Wrong answer0/47ms2640 KiB
14Time limit exceeded0/4449ms1712 KiB
15Time limit exceeded0/4495ms1848 KiB
16Accepted4/49ms3344 KiB
17Time limit exceeded0/4481ms2156 KiB