251622026-02-18 09:58:14szabelrTársaság (50)cpp17Elfogadva 50/5013ms820 KiB
#include <iostream>
#include <vector>
using namespace std;
long long dfs(int v,
         vector<vector<pair<int, int>>>& adj,
         int &count,long long x
         )
{
    long long maxi=0;
    for(auto to:adj[v])
    {

        long long child = dfs(to.first,adj,count,x)+to.second;
        if(child>x)
            count++;
        else
            if(child>maxi) maxi=child;
    }
    return maxi;

}
bool joe(long long x,
         vector<vector<pair<int, int>>>& adj,
         int n,int k)
{
    int count=0;
    dfs(1,adj,count,x);
    if(count<=k)
        return true;
    else
        return false;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,k;
    cin >> n>>k;
    vector<vector<pair<int, int>>> adj(n + 1);
    for (int i = 2; i <= n; i++)
    {
        int a, b;
        cin >> a >> b;
        adj[a].push_back({ i,b });
    }
    long long cmin = 0;
    long long cmax = 1000000000000000;
    long long res=-1;
    while (cmin <= cmax)
    {
        long long mid = (cmin + cmax) / 2;
        if (joe(mid, adj,n,k))
        {
            res=mid;
            cmax = mid - 1;
        }
        else
            cmin = mid + 1;
    }
    cout<<res;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms316 KiB
2Elfogadva0/07ms564 KiB
3Elfogadva3/31ms508 KiB
4Elfogadva3/31ms316 KiB
5Elfogadva3/31ms316 KiB
6Elfogadva3/31ms508 KiB
7Elfogadva3/31ms316 KiB
8Elfogadva3/32ms316 KiB
9Elfogadva3/33ms316 KiB
10Elfogadva3/34ms372 KiB
11Elfogadva3/34ms564 KiB
12Elfogadva3/38ms572 KiB
13Elfogadva4/48ms588 KiB
14Elfogadva4/48ms564 KiB
15Elfogadva4/49ms564 KiB
16Elfogadva4/412ms572 KiB
17Elfogadva4/413ms820 KiB