| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 | 
|---|---|---|---|---|---|---|---|
| 1144338 | KluydQ | 경주 (Race) (IOI11_race) | C++20 | 0 ms | 0 KiB | 
#include <bits/stdc++.h>
 
#define respagold ios_base::sync_with_stdio(0), cin.tie(0);
#define ll long long
#define int long long
#define int2 __int128_t
#define FOR( i, x, n, d ) for( int i = x; i <= n; i += d )
#define FORR( i, x, n, d ) for( int i = x; i >= n; i -= d )
#define F first
#define S second
#define all(x) x.begin(), x.end()
//#define sz(x) (int)(x.size())
#define pb push_back
#define ins insert
#define lb lower_bound
#define ub upper_bound
#define pii pair <int, int>
#define mkp make_pair
 
using namespace std;
 
const int N = 3e5 + 123;
const int inf = 2e18;
const int MOD1 = 1e9 + 7;
const int MOD = 998244353;
const int P = 6547;
 
int a[N], b[N], n, m, k, L, R, x, y, z, w, siz, was_centroid[N], sz[N], pa[N], ans;
vector <int> g[N];
map <pii, int> mp;
int cnt[1000005];
 
mt19937 rng( chrono::steady_clock::now().time_since_epoch().count());
 
int rand( int l, int r )
{
    uniform_int_distribution <int> uid( l, r );
    return uid( rng );
}
void dfs( int v, int p )
{
	sz[v] = 1;
	
	for( auto to : g[v] ) 
	{
		if( to == p || was_centroid[to] ) continue;
		dfs( to, v ), sz[v] += sz[to];
	}
}
int find_centroid( int v, int p )
{
	for( auto to : g[v] )
	{
		if( to == p || was_centroid[to] ) continue;
		if( sz[to] * 2 > siz ) return find_centroid( to, v );
	}
	return v;
}
void add( int v, int p, int d, int op )
{
	d += mp[{v, p}];
	if( d <= k && cnt[d] != -1 ) ans = min( ans, op + cnt[d] );
	
	for( auto to : g[v] ) 
	{
		if( to == p || was_centroid[to] ) continue;
		add( to, v, d, op + 1 );
	}
}
void upd( int v, int p, int d, int tp, int op )
{
	d += mp[{v, p}];
	
	if( d <= k ) 
	{
		if( tp == -1 ) cnt[d] = -1;
		else cnt[d] = ( cnt[d] == -1 ? op : min( cnt[d], op ));
	}
	for( auto to : g[v] ) 
	{
		if( to == p || was_centroid[to] ) continue;
		upd( to, v, d, tp, op + 1 );
	}
}
void centroid_decomposition( int v )
{
	dfs(v, 0); siz = sz[v];
	v = find_centroid(v, 0);
	was_centroid[v] = 1; cnt[0] = 0;
	
	for( auto to : g[v] )
	{
		if( !was_centroid[to] )
		{
			add( to, v, 0, 1 );
			upd( to, v, 0, 1, 1 );
		}
	}
	for( auto to : g[v] ) if( !was_centroid[to] ) upd( to, v, 0, -1, 1 );
	for( auto to : g[v] ) if( !was_centroid[to] ) centroid_decomposition(to);
}
void solve()
{
	cin >> n >> k;
	
	FOR( i, 1, n - 1, 1 ) cin >> a[i] >> b[i];
	memset( cnt, -1, sizeof cnt );
	
	FOR( i, 1, n - 1, 1 )
	{
		cin >> z; x = a[i] + 1, y = b[i] + 1;
		g[x].pb(y), g[y].pb(x);
		mp[{x, y}] = mp[{y, x}] = z;
	}
	ans = inf;
	centroid_decomposition(1);
	cout << (ans == inf ? -1 : ans) << '\n';
}
signed main() 
{
//	freopen("loops.in", "r", stdin);
//	freopen("loops.out", "w", stdout);
	    
    respagold
    
    int test = 1;
    
    if( !test ) cin >> test;
	
	while( test -- )
	{
		solve();
	}
}
// solved by KluydQ
