Submission #1228538

#TimeUsernameProblemLanguageResultExecution timeMemory
1228538tkm_algorithmsRace (IOI11_race)C++20
0 / 100
2 ms5184 KiB
/**
*    In the name of Allah
*    We are nothing and you're everything
*    Ya Muhammad!
**/
#include <bits/stdc++.h>
#include "race.h"
//#include "grader.cpp"

using namespace std;
using ll = long long;

#define all(x) begin(x), end(x)
//#define int ll
const int NN = 2e5+5;

vector<pair<ll, ll>> g[NN];
ll sz[NN], dep[NN];
ll sum[NN];
vector<ll> *vec[NN];
map<ll, ll> *mp[NN];
ll res = NN;

void calc(int a = 0, int p = 0) {
	sz[a] = 1;
	for (auto i: g[a]) {
		if (i.first == p)continue;
		dep[i.first] = dep[a]+1;
		sum[i.first] = sum[a]+i.second;
		calc(i.first, a);
		sz[a] += sz[i.first];
	}
}

int kk;

void dfs(int a = 0, int p = 0, bool ok = true) {
	int mx = 0, big = -1;
	for (auto i: g[a]) {
		if (i.first != p && sz[i.first] > mx)mx = sz[i.first], big = i.first;
	}
	
	for (auto i: g[a]) {
		if (i.first != p && i.first != big)dfs(i.first, a, 0);
	}
	
	if (~big) {
		dfs(big, a, 1), mp[a] = mp[big], vec[a] = vec[big];
	}
	else mp[a] = new map<ll, ll>, vec[a] = new vector<ll>;
	
	vec[a]->push_back(a);
	
	(*mp[a])[sum[a]] = ((*mp[a])[sum[a]]==0?dep[a]: min(dep[a], (*mp[a])[sum[a]]));
	
	map<ll, ll> temp = (*mp[a]);
	if (temp[kk+sum[a]] != 0)res = min(res, temp[kk+sum[a]]-dep[a]);
	for (auto i: g[a]) {
		if (i.first == p || i.first == big)continue;
		for (auto j: *vec[i.first]) {
			vec[a]->push_back(j);
			int h = sum[j]-sum[a];
			if ((*mp[a])[sum[j]] == 0)(*mp[a])[sum[j]] = dep[j];
			else ((*mp[a])[sum[j]]) = min((*mp[a])[sum[j]], dep[j]);
			if (kk-h < 0)continue;
			if (temp[sum[a]+kk-h+sum[a]] == 0)continue;
			else res = min(res, temp[sum[a]+kk-h+sum[a]]+dep[j]-2*dep[a]);
		}
		temp = (*mp[a]);
	}
	
	if (!ok) {
		for (auto i: *vec[a])(*mp[a])[sum[i]] = 0;
	}
}
 
int best_path(int n, int k, int h[][2], int l[]) {
	for (int i = 0; i < n; ++i) {
		g[i].clear();
		(*mp[i]).clear();
		(*vec[i]).clear();
	}
	dep[0] = 1;
	kk = k;
	
	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]});
	}
	
	res = NN;
	calc();
	dfs();
	
	return (res==NN?-1:res);
}

//int main() {
	//int n; cin >> n >> kk;
	//for (int i = 0; i < n-1; ++i) {
		//int a, b, w; cin >> a >> b >> w;
		//g[a].push_back({b, w});
		//g[b].push_back({a, w});
	//}
	
	//dep[0] = 1;
	//calc();
	//dfs();
	//cout << res;
//}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...