55682023-07-29 11:46:411478Társaság (50)cpp17Wrong answer 3/50469ms6060 KiB
#include <bits/stdc++.h>  
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
 
int main()
{
    //ifstream cin("in.txt");

    int n;
    cin >> n;
    //cout << n << '\n';

    int k;
    cin >> k;

    vector<vector<pair<int, int>>> al(n + 1, vector<pair<int, int>>(1));
    for(int i = 2; i <= n; i++){
        int boss, d;
        cin >> boss >> d;
        al[i][0] = {boss, d};
        al[boss].push_back({i, d});
    }
    pair<int, int> farthestNode;
    for(int i = 1; i <= k + 1; i++){
        //leghosszabb ut kereses
        
        farthestNode = {0, 0};
        vector<bool> seen(n + 1);
        vector<int> dD(n + 1);

        for(int i = 1; i <= n; i++){
            if(!seen[i]){
                queue<int> q;
                q.push(i);

                dD[i] = 0;
                seen[i] = 1;

                while(!q.empty()){
                    int a = q.front();
                    q.pop();
                    for(int i = 0; i < al[a].size(); i++){
                        if(!seen[al[a][i].first] && al[al[a][i].first][0].first != 0){
                            seen[al[a][i].first] = 1;

                            dD[al[a][i].first] = dD[a] + al[a][i].second;
                            q.push(al[a][i].first);

                            if(dD[al[a][i].first] > farthestNode.second){
                                farthestNode.first = al[a][i].first;
                                farthestNode.second = dD[al[a][i].first];
                            }
                        }
                    }
                }
            }
        }

        
        int u = farthestNode.first;
        int cutVal = INT_MAX;
        int cutWhere;
        int dU = 0;
        
        while(al[u][0].first != 0){
            if(max(dD[u] - al[u][0].second, dU) < cutVal){
                cutVal = max(dD[u] - al[u][0].second, dU);
                cutWhere = u;
            }

            dU += al[u][0].second;
            u = al[u][0].first;
        }

        al[cutWhere][0].first = 0;

        //cout << farthestNode.first << " " << farthestNode.second << '\n';
        
        //cout << cutWhere << '\n';
        
    }

    cout << farthestNode.second << '\n';

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base3/50
1Accepted0/03ms1812 KiB
2Wrong answer0/0185ms2736 KiB
3Wrong answer0/32ms2020 KiB
4Wrong answer0/33ms2260 KiB
5Accepted3/33ms2480 KiB
6Wrong answer0/33ms2732 KiB
7Wrong answer0/34ms3096 KiB
8Wrong answer0/37ms3336 KiB
9Wrong answer0/324ms3776 KiB
10Wrong answer0/3273ms3948 KiB
11Wrong answer0/310ms4336 KiB
12Time limit exceeded0/3462ms3400 KiB
13Wrong answer0/4328ms4776 KiB
14Wrong answer0/445ms5192 KiB
15Wrong answer0/423ms5448 KiB
16Time limit exceeded0/4469ms4604 KiB
17Wrong answer0/463ms6060 KiB