Submission #385128

# Submission time Handle Problem Language Result Execution time Memory
385128 2021-04-03T08:36:19 Z hivakarami Race (IOI11_race) C++14
100 / 100
1641 ms 50020 KB
#include "race.h"
#include<bits/stdc++.h>
 
using namespace std;
 
typedef long long int ll;
typedef long double ld;
#define f first
#define s second
 
const int N = 2e5 + 100;
const int lg = 18;
const int inf = 1e9 + 10;

int ans = inf, W[N], w[N];
int n, k, h[N], par[N], sz[N];
vector<int> v, vec;
vector<pair<int, int>> adj[N];
map<ll, int> mn;
bool dead[N];


void DFS(int u)
{
	v.push_back(u);
	for(auto it : adj[u])
	{
		int x = it.f;
		if(!dead[x] && x != par[u])
			DFS(x);
	}
}

void dfs(int u)
{
	sz[u] = 1;
	vec.push_back(u);
	for(auto it : adj[u])
	{
		int x = it.f, id = it.s;
		if(!dead[x] && x != par[u])
		{
			par[x] = u;
			h[x] = h[u] + 1;
			W[x] = W[u] + w[id];
			dfs(x);
			sz[u] += sz[x];
		}
	}
}


inline void cl()
{
	for(auto u : vec)
	{
		par[u] = -1;
		sz[u] = h[u] = W[u] = 0;
	}
	vec.clear();
}


int centroid(int u)
{
	for(auto it : adj[u])
	{
		int x = it.f;
		if(!dead[x] && x != par[u] && sz[x] > int(vec.size())/2)
			return centroid(x);
	}
	return u;
}


void solve(int u)
{
	cl();
	dfs(u);
	u = centroid(u);
	cl();
	dfs(u);
	//cout << ".." << u << ' ' << vec.size() << endl;
	mn.clear();
	dead[u] = true;
	
	for(auto it : adj[u])
	{
		int x = it.f;

		if(dead[x])
			continue;
			
		v.clear();
		DFS(x);
		
		//cout << "X" << x << endl;
		
		for(auto y : v)
		{
			//cout << y << endl;
			if(k >= W[y] && mn.find(k - W[y]) != mn.end())
			{
				ans = min(ans, h[y] + mn[k - W[y]]);
				//cout << x << ' ' << y << ' ' << h[y] << ' ' << k - W[y] << ' ' << mn[k - W[y]] << endl;
			}
		}
		
		for(auto y : v)
		{
			if(mn.find(W[y]) != mn.end())
				mn[W[y]] = min(mn[W[y]], h[y]);
			else
				mn[W[y]] = h[y];
		}
	}
	
	if(mn.find(k) != mn.end())
	{
		ans = min(ans, mn[k]);
		//cout << "mn[k]" << mn[k] << endl;
	}
	
	
	for(auto it : adj[u])
	{
		int x = it.f;
		if(!dead[x])
			solve(x);
	}
	
	
	
}




//*

int best_path(int N, int K, int H[][2], int L[])
{
	n = N;
	k = K;
	
	for(int i = 0; i < n-1; i++)
	{
		int x = H[i][0], y = H[i][1];
		adj[x].push_back({y, i});
		adj[y].push_back({x, i});
	}	
	for(int i = 0; i < n-1; i++)
		w[i] = L[i];
	
	
	solve(0);

	if(ans >= inf)
		ans = -1;
	
	return ans;
	
}

//*/







/*
int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);	

	cin >> n >> k;
	
	for(int i = 0; i < n-1; i++)
	{
		int x, y;
		cin >> x >> y;
		adj[x].push_back({y, i});
		adj[y].push_back({x, i});
	}	
	for(int i = 0; i < n-1; i++)
		cin >> w[i];
	
	
	solve(0);
	
	if(ans == inf)
		ans = -1;
	
	cout << ans << endl;

	return 0;
	
}

//*/

 
/*
3 2
0 1
1 2
1 1
*/ 
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5228 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5228 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 4 ms 5120 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 6 ms 5100 KB Output is correct
22 Correct 6 ms 5228 KB Output is correct
23 Correct 6 ms 5228 KB Output is correct
24 Correct 6 ms 5228 KB Output is correct
25 Correct 6 ms 5228 KB Output is correct
26 Correct 6 ms 5228 KB Output is correct
27 Correct 5 ms 5100 KB Output is correct
28 Correct 6 ms 5228 KB Output is correct
29 Correct 6 ms 5228 KB Output is correct
30 Correct 6 ms 5228 KB Output is correct
31 Correct 6 ms 5228 KB Output is correct
32 Correct 6 ms 5228 KB Output is correct
33 Correct 8 ms 5228 KB Output is correct
34 Correct 7 ms 5228 KB Output is correct
35 Correct 6 ms 5228 KB Output is correct
36 Correct 6 ms 5228 KB Output is correct
37 Correct 5 ms 5228 KB Output is correct
38 Correct 7 ms 5228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5228 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 427 ms 12772 KB Output is correct
20 Correct 423 ms 12900 KB Output is correct
21 Correct 414 ms 12772 KB Output is correct
22 Correct 347 ms 12924 KB Output is correct
23 Correct 443 ms 13032 KB Output is correct
24 Correct 204 ms 12652 KB Output is correct
25 Correct 365 ms 16492 KB Output is correct
26 Correct 171 ms 20076 KB Output is correct
27 Correct 424 ms 20072 KB Output is correct
28 Correct 1573 ms 50020 KB Output is correct
29 Correct 1523 ms 48864 KB Output is correct
30 Correct 419 ms 22888 KB Output is correct
31 Correct 433 ms 23016 KB Output is correct
32 Correct 620 ms 23016 KB Output is correct
33 Correct 903 ms 22496 KB Output is correct
34 Correct 1425 ms 35424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5228 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 4 ms 5120 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 6 ms 5100 KB Output is correct
22 Correct 6 ms 5228 KB Output is correct
23 Correct 6 ms 5228 KB Output is correct
24 Correct 6 ms 5228 KB Output is correct
25 Correct 6 ms 5228 KB Output is correct
26 Correct 6 ms 5228 KB Output is correct
27 Correct 5 ms 5100 KB Output is correct
28 Correct 6 ms 5228 KB Output is correct
29 Correct 6 ms 5228 KB Output is correct
30 Correct 6 ms 5228 KB Output is correct
31 Correct 6 ms 5228 KB Output is correct
32 Correct 6 ms 5228 KB Output is correct
33 Correct 8 ms 5228 KB Output is correct
34 Correct 7 ms 5228 KB Output is correct
35 Correct 6 ms 5228 KB Output is correct
36 Correct 6 ms 5228 KB Output is correct
37 Correct 5 ms 5228 KB Output is correct
38 Correct 7 ms 5228 KB Output is correct
39 Correct 427 ms 12772 KB Output is correct
40 Correct 423 ms 12900 KB Output is correct
41 Correct 414 ms 12772 KB Output is correct
42 Correct 347 ms 12924 KB Output is correct
43 Correct 443 ms 13032 KB Output is correct
44 Correct 204 ms 12652 KB Output is correct
45 Correct 365 ms 16492 KB Output is correct
46 Correct 171 ms 20076 KB Output is correct
47 Correct 424 ms 20072 KB Output is correct
48 Correct 1573 ms 50020 KB Output is correct
49 Correct 1523 ms 48864 KB Output is correct
50 Correct 419 ms 22888 KB Output is correct
51 Correct 433 ms 23016 KB Output is correct
52 Correct 620 ms 23016 KB Output is correct
53 Correct 903 ms 22496 KB Output is correct
54 Correct 1425 ms 35424 KB Output is correct
55 Correct 31 ms 6252 KB Output is correct
56 Correct 24 ms 5996 KB Output is correct
57 Correct 192 ms 14312 KB Output is correct
58 Correct 63 ms 14304 KB Output is correct
59 Correct 383 ms 26092 KB Output is correct
60 Correct 1566 ms 48224 KB Output is correct
61 Correct 489 ms 24892 KB Output is correct
62 Correct 425 ms 23012 KB Output is correct
63 Correct 633 ms 23140 KB Output is correct
64 Correct 1641 ms 29544 KB Output is correct
65 Correct 1386 ms 36324 KB Output is correct
66 Correct 1567 ms 46432 KB Output is correct
67 Correct 227 ms 24540 KB Output is correct
68 Correct 809 ms 34528 KB Output is correct
69 Correct 801 ms 34752 KB Output is correct
70 Correct 741 ms 33248 KB Output is correct