Submission #488732

# Submission time Handle Problem Language Result Execution time Memory
488732 2021-11-20T09:00:39 Z ssense Race (IOI11_race) C++14
100 / 100
1143 ms 47528 KB
#include <bits/stdc++.h>
#include "race.h"
typedef long long  ll;
using namespace std;
#define vint vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define MOD2 998244353
#define MX 1000000000
#define MXL 1000000000000000000
#define PI (ld)2*acos(0.0)
#define nax 200005
#define pb push_back
#define sc second
#define fr first
//#define int long long
//#define endl '\n'
#define ld long double
#define NO cout << "NO" << endl
#define YES cout << "YES" << endl
vector<pair<int, ll>> adj[200005];
int n_glob;
int k_glob;
int size_sub[200005];
int del[200005];
 
void calc(int u, int par)
{
	size_sub[u] = 1;
	for(auto v : adj[u])
	{
		if(del[v.fr] == 0 && v.fr != par)
		{
			calc(v.fr, u);
			size_sub[u]+=size_sub[v.fr];
		}
	}
}
 
int find_centroid(int u, int size, int par)
{
	for(auto v : adj[u])
	{
		if(v.fr != par && del[v.fr] == 0 && size_sub[v.fr] > size)
		{
			return find_centroid(v.fr, size, u);
		}
	}
	return u;
}
 
int ans = INT_MAX;
map<ll, int> cnt;

void dfs(int u, int par, ll weight, int type, int depth){
	if(weight > k_glob)return;
	if(type == 0){
		if(cnt.count(k_glob - (int)weight)){
			ans = min(ans, cnt[k_glob - weight] + depth);
		}
	}
	else{
		if(!cnt.count((int)weight))cnt[(int)weight] = depth;
		else cnt[weight] = min(cnt[weight], depth);
	}
 
	for(auto x: adj[u]){
		ll v = x.first, w = x.second;
		if(v == par || del[v])continue;
		dfs((int)v, u, weight + w, type, depth + 1);
	}
}
 
void assign(int u)
{
	calc(u, -1);
	int centroid = find_centroid(u, size_sub[u] / 2, -1);
	del[centroid] = 1;
	cnt.clear();
	cnt[0] = 0;
	for(auto x : adj[centroid])
	{
		if(del[x.fr] == 1)
		{
			continue;
		}
		dfs(x.fr, -1, x.sc, 0, 1);
		dfs(x.fr, -1, x.sc, 1, 1);
	}
	for(auto x : adj[centroid])
	{
		if(del[x.fr] == 0)
		{
			assign(x.fr);
		}
	}
}
 
int best_path(int n, int k, int h[][2],int l[])
{
	ans = INT_MAX;
	n_glob = n;
	k_glob = k;
	for(int i = 0; i <= n; i++)
	{
		adj[i].clear();
		del[i] = 0;
		size_sub[i] = 0;
	}
	cnt.clear();
	for(int i = 0; i < n-1; i++)
	{
		adj[h[i][0]].pb({h[i][1], l[i]});
		adj[h[i][1]].pb({h[i][0], l[i]});
	}
	assign(0);
	if(ans == INT_MAX)
	{
		return -1;
	}
	return ans;
}
/*
int32_t main(){
	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];
	}
	for(int i = 0; i < n-1; i++)
	{
		cin >> l[i];
	}
	cout << best_path(n, k, h, l) << endl;
}
 */
/*
3 3
0 1
0 2
1 1
 
4 3
0 1
1 2
1 3
1 2 4
 
11 12
0 1
0 2
2 3
3 4
4 5
0 6
6 7
6 8
8 9
8 10
3 4 5 4 6 3 2 5 6 7
 */
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 5004 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 3 ms 5028 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 4980 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 5008 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 5032 KB Output is correct
15 Correct 4 ms 5004 KB Output is correct
16 Correct 4 ms 4940 KB Output is correct
17 Correct 4 ms 4940 KB Output is correct
18 Correct 3 ms 5004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 5004 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 3 ms 5028 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 4980 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 5008 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 5032 KB Output is correct
15 Correct 4 ms 5004 KB Output is correct
16 Correct 4 ms 4940 KB Output is correct
17 Correct 4 ms 4940 KB Output is correct
18 Correct 3 ms 5004 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 4 ms 5012 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 3 ms 5068 KB Output is correct
23 Correct 3 ms 5068 KB Output is correct
24 Correct 3 ms 5112 KB Output is correct
25 Correct 5 ms 5068 KB Output is correct
26 Correct 3 ms 4992 KB Output is correct
27 Correct 3 ms 5068 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 4 ms 5116 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 5 ms 5144 KB Output is correct
33 Correct 4 ms 5068 KB Output is correct
34 Correct 5 ms 5068 KB Output is correct
35 Correct 5 ms 5108 KB Output is correct
36 Correct 5 ms 5068 KB Output is correct
37 Correct 4 ms 5068 KB Output is correct
38 Correct 4 ms 5140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 5004 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 3 ms 5028 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 4980 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 5008 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 5032 KB Output is correct
15 Correct 4 ms 5004 KB Output is correct
16 Correct 4 ms 4940 KB Output is correct
17 Correct 4 ms 4940 KB Output is correct
18 Correct 3 ms 5004 KB Output is correct
19 Correct 204 ms 12556 KB Output is correct
20 Correct 228 ms 12560 KB Output is correct
21 Correct 235 ms 12572 KB Output is correct
22 Correct 234 ms 12596 KB Output is correct
23 Correct 104 ms 12888 KB Output is correct
24 Correct 110 ms 12152 KB Output is correct
25 Correct 140 ms 16284 KB Output is correct
26 Correct 103 ms 19980 KB Output is correct
27 Correct 218 ms 20600 KB Output is correct
28 Correct 329 ms 34880 KB Output is correct
29 Correct 312 ms 33740 KB Output is correct
30 Correct 230 ms 22476 KB Output is correct
31 Correct 258 ms 22528 KB Output is correct
32 Correct 254 ms 22588 KB Output is correct
33 Correct 288 ms 21348 KB Output is correct
34 Correct 237 ms 21316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 5004 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 3 ms 5028 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 4980 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 5008 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 5032 KB Output is correct
15 Correct 4 ms 5004 KB Output is correct
16 Correct 4 ms 4940 KB Output is correct
17 Correct 4 ms 4940 KB Output is correct
18 Correct 3 ms 5004 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 4 ms 5012 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 3 ms 5068 KB Output is correct
23 Correct 3 ms 5068 KB Output is correct
24 Correct 3 ms 5112 KB Output is correct
25 Correct 5 ms 5068 KB Output is correct
26 Correct 3 ms 4992 KB Output is correct
27 Correct 3 ms 5068 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 4 ms 5116 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 5 ms 5144 KB Output is correct
33 Correct 4 ms 5068 KB Output is correct
34 Correct 5 ms 5068 KB Output is correct
35 Correct 5 ms 5108 KB Output is correct
36 Correct 5 ms 5068 KB Output is correct
37 Correct 4 ms 5068 KB Output is correct
38 Correct 4 ms 5140 KB Output is correct
39 Correct 204 ms 12556 KB Output is correct
40 Correct 228 ms 12560 KB Output is correct
41 Correct 235 ms 12572 KB Output is correct
42 Correct 234 ms 12596 KB Output is correct
43 Correct 104 ms 12888 KB Output is correct
44 Correct 110 ms 12152 KB Output is correct
45 Correct 140 ms 16284 KB Output is correct
46 Correct 103 ms 19980 KB Output is correct
47 Correct 218 ms 20600 KB Output is correct
48 Correct 329 ms 34880 KB Output is correct
49 Correct 312 ms 33740 KB Output is correct
50 Correct 230 ms 22476 KB Output is correct
51 Correct 258 ms 22528 KB Output is correct
52 Correct 254 ms 22588 KB Output is correct
53 Correct 288 ms 21348 KB Output is correct
54 Correct 237 ms 21316 KB Output is correct
55 Correct 20 ms 6032 KB Output is correct
56 Correct 15 ms 5848 KB Output is correct
57 Correct 151 ms 13760 KB Output is correct
58 Correct 45 ms 12640 KB Output is correct
59 Correct 291 ms 25528 KB Output is correct
60 Correct 1045 ms 47528 KB Output is correct
61 Correct 318 ms 22856 KB Output is correct
62 Correct 280 ms 22676 KB Output is correct
63 Correct 420 ms 22784 KB Output is correct
64 Correct 1143 ms 28100 KB Output is correct
65 Correct 250 ms 21992 KB Output is correct
66 Correct 642 ms 33296 KB Output is correct
67 Correct 167 ms 21344 KB Output is correct
68 Correct 419 ms 26904 KB Output is correct
69 Correct 439 ms 26508 KB Output is correct
70 Correct 424 ms 26180 KB Output is correct