Submission #483975

#TimeUsernameProblemLanguageResultExecution timeMemory
483975wind_reaperRace (IOI11_race)C++17
0 / 100
3 ms7372 KiB
#include <bits/stdc++.h>
#ifndef LOCAL
#include "race.h"
#endif

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
*/

using namespace std;
// using namespace __gnu_pbds;
using namespace chrono;

// mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
/*
template <class T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
*/

//***************** CONSTANTS *****************

const int MXN = 100000;

//***************** GLOBAL VARIABLES *****************

vector<array<int, 2>> g[MXN];
map<int, int> mp[MXN];
int ans = INT_MAX, K;

//***************** AUXILIARY STRUCTS *****************



//***************** MAIN BODY *****************

void dfs(int p, int u){
	mp[u][0] = 0;

	for(auto& [v, d] : g[u]) if(v != p){
		dfs(u, v);

		if(mp[u].size() < mp[v].size())
			swap(mp[u], mp[v]);

		for(auto& [t, l] : mp[v]){
			if(t + d > K) continue;

			int T = t + d;

			if(mp[u].find(K - T) != mp[u].end()) ans = min(ans, mp[u][K - T] + l + 1);

			if(mp[u].find(T) != mp[u].end()) mp[u][T] = min(mp[u][T], l + 1);
			else mp[u][T] = l + 1; 
		}
	}

	// cout << u << '\n';
	// for(auto& [t, l] : mp[u])
	// 	cout << t << ' ' << l << '\n';

	// cout << "***\n";
}

int best_path(int N, int k, int H[][2], int L[]){
	for(int i = 0; i < N - 1; i++){
		g[H[i][0]].push_back({H[i][1], L[i]});
		g[H[i][1]].push_back({H[i][0], L[i]});
	}

	K = k;

	dfs(0, 0);

	return (ans == INT_MAX ? -1 : ans);
}

//***************** *****************

#ifdef LOCAL
int32_t main(){
	ios_base::sync_with_stdio(NULL);
	cin.tie(NULL);

	#ifdef LOCAL
		auto begin = high_resolution_clock::now();
	#endif

	int tc = 1;
	// cin >> tc; 
	for (int t = 0; t < tc; t++){
		int N, k;
		cin >> N >> k;

		int H[N-1][2], L[N-1];
		for(int i = 0; i < N-1; i++)
			cin >> H[i][0] >> H[i][1] >> L[i];

		cout << best_path(N, k, H, L) << '\n';
	}

	#ifdef LOCAL 
		auto end = high_resolution_clock::now();
		cout << fixed << setprecision(4);
		cout << "Execution Time: " << duration_cast<duration<double>>(end - begin).count() << "seconds" << endl;
	#endif

	return 0;
}
#endif
/*
If code gives a WA, check for the following : 
1. I/O format

2. Are you clearing all global variables in between tests if multitests are a thing

3. Can you definitively prove the logic

4. If the code gives an inexplicable TLE, and you are sure you have the best possible complexity,
use faster io
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...