42532023-03-19 17:51:57horvathabelTársaság (50)cpp17Wrong answer 3/50500ms5176 KiB
#include <bits/stdc++.h>
using namespace std;
vector<pair<int,int>> g[10001];
pair<int, int> p[10001];
vector<int> t;
int ans=0; 
vector<bool> seen;
int bfsmx(int x){
	queue<int> q; 
	q.push(x);
	int t[10001];
	t[x]=0; 
	int mx=0;
	while (!q.empty()){
		int v=q.front();
		q.pop(); 
		for (auto edge:g[v]) {
			t[edge.first]=t[v]+edge.second; 
			mx=max(t[edge.first], mx);
			q.push(edge.first);
		}
	}
	return mx; 
}  
void dfs(int x, int m){
	seen[x]=true;
	if (p[x].first!=-1){
	if (t[x]+p[x].second>m){
		t[x]=0;
		ans++;	
	}
	t[p[x].first]=t[x]+p[x].second; 
	if (!seen[p[x].first]) dfs(p[x].first, m);
}
}
int main() {
	int n, k;
	cin>>n>>k;
	vector<int> cn;
	cn.assign(n+1, 0);
	for (int i=2; i<=n;i++){
		int x,y;
		cin>>x>>y;
		g[x].push_back({i,y});
		p[i]={x,y};
		cn[x]++;
	}
	vector<int> nulla; 

	for (int i=1;i<=n;i++){
		if (cn[i]==0) nulla.push_back(i);  
	}
	p[1]={-1,-1};
	int l=0; 
	int r=bfsmx(1);
	int mego;
	while (l+1<r){
		int m=(l+r)/2;
		seen.assign(n+1, false);
		t.assign(10001,10e9); 
		for (int x: nulla){
			t[x]=0; 
			dfs(x,m);
			
		}
		if (ans<=k){
			mego=m; 
			r=m;
		}
		else{
			l=m; 
		}
		ans=0; 
		t.clear();
		seen.clear();
	}
	cout<<mego; 

}
SubtaskSumTestVerdictTimeMemory
base3/50
1Accepted0/03ms2512 KiB
2Wrong answer0/08ms3120 KiB
3Accepted3/33ms3028 KiB
4Wrong answer0/33ms3340 KiB
5Wrong answer0/33ms3556 KiB
6Wrong answer0/33ms3772 KiB
7Wrong answer0/33ms3776 KiB
8Wrong answer0/34ms3840 KiB
9Wrong answer0/34ms3916 KiB
10Wrong answer0/36ms4204 KiB
11Wrong answer0/37ms4204 KiB
12Wrong answer0/39ms4488 KiB
13Wrong answer0/410ms4600 KiB
14Wrong answer0/410ms4904 KiB
15Time limit exceeded0/4474ms3720 KiB
16Wrong answer0/414ms5176 KiB
17Time limit exceeded0/4500ms3880 KiB