188512025-11-07 10:41:33ubormaciTársaság (50)cpp17Hibás válasz 7/506ms1260 KiB
#include <iostream>
#include <algorithm> // for sort, mainly
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <array>
#include <string>
#include <cstdio>
#include <iterator>
#include <unordered_set>
#include <cstdint> // for int64_t, int32_t, etc
#include <queue>
#include <stack>
#include <deque>
#include <numeric> // gcd, lcm
#include <fstream>
#include <bitset> // for bitset
#include <iomanip>
#include <cassert> // for set with custom ordering
#include <type_traits> // for set with custom ordering
#include <utility> // for swap, forward, etc
using namespace std;

#pragma GCC optimize("O2")
// #pragma GCC optimize("O1","O2","O3","Ofast","unroll-loops")
// #pragma GCC target("sse","sse2","sse3","sse4.1","sse4.2","avx","avx2","fma")

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

/*

notes:

int64_t
stoi(string s) -> string to int
to_string() -> int (or else) to string

vector declaration:
vector<ll> v(n, 0)
vector<vector<ll>> v(n, vector<ll>(n, 0));

{if statement} ? {truth value} : {false value}

#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif

std::lcm(ll a, ll b), std::gcd(int a, int b)

cout << fixed << setprecision(n);

set with custom ordering
set<ll, decltype(&cmp)> qu(cmp);

*/

typedef int64_t ll;

void solve() {

	ll n, k;
	cin >> n >> k;
	vector<pair<ll,ll>> p(n+1, {0,0});
	vector<pair<ll,ll>> del(n-1, {0,0});
	ll dj = 0;
	
	vector<ll> ch(n+1, 0);

	vector<bool> leaf(n+1, true);

	for(ll i = 2; i <= n; i++) {
		cin >> p[i].first >> p[i].second;

		leaf[p[i].first] = false;
		ch[p[i].first]++;

		pair<ll,ll> dh = {p[i].second, i};
		del[dj] = dh;
		dj++;
	}

	sort(del.rbegin(), del.rend());
	for(ll i = 0; i < k; i++) {
		
		ll w = del[i].first;
		ll ind = del[i].second;

		// it's parent might become a leaf
		ll parent = p[ind].first;
		ch[parent]--;
		if(ch[parent] == 0) {
			leaf[parent] = true;
		}

		p[ind] = {0, 0};

	}

	queue<ll> q;
	vector<ll> d(n+1, 0);
	vector<bool> vis(n+1, false);
	// max depth

	for(ll i = 1; i <= n; i++) {
		if(leaf[i]) {
			vis[i] = true;
			q.push(i);
		}
	}

	ll mx = 0;

	while(!q.empty()) {

		ll curr = q.front();
		q.pop();

		ll parent = p[curr].first;
		ll w = p[curr].second;

		mx = max(mx, d[curr]);

		if(parent != 0) {

			if(vis[parent] == false) {
				vis[parent] = true;
				q.push(parent);
			}

			d[parent] = max(d[parent], d[curr] + w);

		}

	}

	cout << mx;

}	

int main()
{
	std::ios_base::sync_with_stdio(false);
	//cin.tie(nullptr);
	//cout.tie(nullptr);

	solve();

	return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base7/50
1Elfogadva0/01ms508 KiB
2Hibás válasz0/03ms692 KiB
3Hibás válasz0/31ms508 KiB
4Hibás válasz0/31ms316 KiB
5Elfogadva3/31ms316 KiB
6Hibás válasz0/31ms316 KiB
7Hibás válasz0/31ms316 KiB
8Hibás válasz0/31ms316 KiB
9Hibás válasz0/32ms532 KiB
10Hibás válasz0/33ms440 KiB
11Hibás válasz0/33ms624 KiB
12Hibás válasz0/33ms668 KiB
13Hibás válasz0/44ms824 KiB
14Hibás válasz0/44ms820 KiB
15Hibás válasz0/44ms952 KiB
16Elfogadva4/44ms820 KiB
17Hibás válasz0/46ms1260 KiB