Submission #135737

# Submission time Handle Problem Language Result Execution time Memory
135737 2019-07-24T10:25:58 Z Sorting Race (IOI11_race) C++14
43 / 100
3000 ms 55416 KB
#include <bits/stdc++.h>
//#include "race.h"

using namespace std;

const int N = 2e5 + 7;
const int inf = 1e9;
const int K = 1e6 + 7;

int n, k;
vector <int> adj[N], len[N], adj_c[N];
int sz[N];

bool used[N];
int link[N];

int find_cnt(int u, int p = -1){
	sz[u] = 1;

	for(int to: adj[u]){
		if(p == to || used[to]){
			continue;
		}

		sz[u] += find_cnt(to, u);
	}

	return sz[u];
}

int find_centroid(int u, int cnt, int p = -1){
	for(int to: adj[u]){
		if(to == p || used[to]){
			continue;
		}

		if(sz[to] > (cnt / 2)){
			return find_centroid(to, cnt, u);
		}
	}

	return u;
}	

int root;

void decompose(int u, int p = -1){
	int cnt = find_cnt(u);
	int cen = find_centroid(u, cnt);

	if(p != -1){
		adj_c[p].push_back(cen);
	}
	else{
		root = cen;
	}

	used[cen] = true;
	for(int to: adj[cen]){
		if(!used[to]){
			decompose(to, cen);
		}
	}
	used[cen] = false;
}

int arr[K];

struct vv{
	vector<pair<int, int> > t[N];
	int cnt;

	vv(){
		cnt = 0;
	}

	void add_vector(){
		cnt++;
	}

	void clear(){
		for(int i = 0; i < cnt; i++){
			t[i].clear();
		}
		cnt = 0;
	}

	void push_back(pair<int, int> p){
		t[cnt - 1].push_back(p);
	}

	vector<pair<int, int> >  _back(){
		return t[cnt - 1];
	}
};

vv vec;

void solve_k(int u, int pr, int sum, int d){
	if(sum > k){
		return;
	}

	vec.push_back({sum, d});

	for(int i = 0; i < adj[u].size(); i++){
		int to = adj[u][i];
		int l = len[u][i];

		if(used[to] || to == pr){
			continue;
		}

		solve_k(to, u, sum + l, d + 1);
	}
}

int solve(int u){
	int ans = inf;

	used[u] = true;
	for(int to: adj_c[u]){
		ans = min(ans, solve(to));
	}

	vec.clear();
	for(int i = 0; i < adj[u].size(); i++){
		vec.add_vector();

		solve_k(adj[u][i], u, len[u][i], 1);

		for(pair<int, int> p: vec._back()){
			if(arr[k - p.first] != inf){
				ans = min(ans, p.second + arr[k - p.first]);
			}
		}

		for(pair<int, int> p: vec._back()){
			arr[p.first] = min(arr[p.first], p.second);
		}
	}
	for(int i = 0; i < vec.cnt; i++){
		vector<pair<int, int> > &vec2 = vec.t[i]; 
		for(pair<int, int> p: vec2){
			if(p.first){
				arr[p.first] = inf;
			}
		}
	}
	vec.clear();
	used[u] = false;

	return ans;
}

int best_path(int _n, int _k, int _h[][2], int _l[]){

	n = _n;
	k = _k;

	for(int i = 0; i < n - 1; i++){
		adj[_h[i][0]].push_back(_h[i][1]);
		adj[_h[i][1]].push_back(_h[i][0]);

		len[_h[i][0]].push_back(_l[i]);
		len[_h[i][1]].push_back(_l[i]);
	}

	decompose(0);

	for(int i = 1; i < K; i++){
		arr[i] = inf;
	}

	int ans = solve(root);

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

	return ans;
}

/*int _h[N][2], _l[N];

int main(){
	int _n, _k;

	cin >> _n >> _k;

	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";

 	return 0;
}*/
/*
5 5
0 1 2
1 2 1
2 3 2 
3 4 3
*/
/*
11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
*/

Compilation message

race.cpp: In function 'void solve_k(int, int, int, int)':
race.cpp:106:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < adj[u].size(); i++){
                 ~~^~~~~~~~~~~~~~~
race.cpp: In function 'int solve(int)':
race.cpp:127:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < adj[u].size(); i++){
                 ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23032 KB Output is correct
2 Correct 23 ms 23032 KB Output is correct
3 Correct 23 ms 23032 KB Output is correct
4 Correct 24 ms 23032 KB Output is correct
5 Correct 23 ms 23032 KB Output is correct
6 Correct 23 ms 23032 KB Output is correct
7 Correct 24 ms 23032 KB Output is correct
8 Correct 23 ms 23032 KB Output is correct
9 Correct 23 ms 23032 KB Output is correct
10 Correct 23 ms 23032 KB Output is correct
11 Correct 23 ms 23032 KB Output is correct
12 Correct 23 ms 23032 KB Output is correct
13 Correct 20 ms 23032 KB Output is correct
14 Correct 24 ms 23032 KB Output is correct
15 Correct 23 ms 23032 KB Output is correct
16 Correct 23 ms 23032 KB Output is correct
17 Correct 23 ms 23032 KB Output is correct
18 Correct 23 ms 23032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23032 KB Output is correct
2 Correct 23 ms 23032 KB Output is correct
3 Correct 23 ms 23032 KB Output is correct
4 Correct 24 ms 23032 KB Output is correct
5 Correct 23 ms 23032 KB Output is correct
6 Correct 23 ms 23032 KB Output is correct
7 Correct 24 ms 23032 KB Output is correct
8 Correct 23 ms 23032 KB Output is correct
9 Correct 23 ms 23032 KB Output is correct
10 Correct 23 ms 23032 KB Output is correct
11 Correct 23 ms 23032 KB Output is correct
12 Correct 23 ms 23032 KB Output is correct
13 Correct 20 ms 23032 KB Output is correct
14 Correct 24 ms 23032 KB Output is correct
15 Correct 23 ms 23032 KB Output is correct
16 Correct 23 ms 23032 KB Output is correct
17 Correct 23 ms 23032 KB Output is correct
18 Correct 23 ms 23032 KB Output is correct
19 Correct 28 ms 23004 KB Output is correct
20 Correct 23 ms 23004 KB Output is correct
21 Correct 25 ms 23160 KB Output is correct
22 Correct 30 ms 23160 KB Output is correct
23 Correct 29 ms 23160 KB Output is correct
24 Correct 25 ms 23160 KB Output is correct
25 Correct 33 ms 23160 KB Output is correct
26 Correct 25 ms 23160 KB Output is correct
27 Correct 24 ms 23172 KB Output is correct
28 Correct 31 ms 23160 KB Output is correct
29 Correct 26 ms 23160 KB Output is correct
30 Correct 29 ms 23188 KB Output is correct
31 Correct 41 ms 23288 KB Output is correct
32 Correct 25 ms 23160 KB Output is correct
33 Correct 25 ms 23160 KB Output is correct
34 Correct 27 ms 23160 KB Output is correct
35 Correct 25 ms 23160 KB Output is correct
36 Correct 24 ms 23160 KB Output is correct
37 Correct 24 ms 23160 KB Output is correct
38 Correct 24 ms 23160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23032 KB Output is correct
2 Correct 23 ms 23032 KB Output is correct
3 Correct 23 ms 23032 KB Output is correct
4 Correct 24 ms 23032 KB Output is correct
5 Correct 23 ms 23032 KB Output is correct
6 Correct 23 ms 23032 KB Output is correct
7 Correct 24 ms 23032 KB Output is correct
8 Correct 23 ms 23032 KB Output is correct
9 Correct 23 ms 23032 KB Output is correct
10 Correct 23 ms 23032 KB Output is correct
11 Correct 23 ms 23032 KB Output is correct
12 Correct 23 ms 23032 KB Output is correct
13 Correct 20 ms 23032 KB Output is correct
14 Correct 24 ms 23032 KB Output is correct
15 Correct 23 ms 23032 KB Output is correct
16 Correct 23 ms 23032 KB Output is correct
17 Correct 23 ms 23032 KB Output is correct
18 Correct 23 ms 23032 KB Output is correct
19 Correct 446 ms 34408 KB Output is correct
20 Correct 382 ms 34264 KB Output is correct
21 Correct 432 ms 35188 KB Output is correct
22 Correct 700 ms 36264 KB Output is correct
23 Correct 226 ms 34016 KB Output is correct
24 Correct 189 ms 33588 KB Output is correct
25 Correct 322 ms 36964 KB Output is correct
26 Correct 223 ms 40616 KB Output is correct
27 Correct 1444 ms 45764 KB Output is correct
28 Correct 640 ms 55416 KB Output is correct
29 Correct 661 ms 54900 KB Output is correct
30 Correct 1433 ms 45736 KB Output is correct
31 Correct 1414 ms 45816 KB Output is correct
32 Correct 1516 ms 45908 KB Output is correct
33 Correct 615 ms 45412 KB Output is correct
34 Correct 525 ms 46476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23032 KB Output is correct
2 Correct 23 ms 23032 KB Output is correct
3 Correct 23 ms 23032 KB Output is correct
4 Correct 24 ms 23032 KB Output is correct
5 Correct 23 ms 23032 KB Output is correct
6 Correct 23 ms 23032 KB Output is correct
7 Correct 24 ms 23032 KB Output is correct
8 Correct 23 ms 23032 KB Output is correct
9 Correct 23 ms 23032 KB Output is correct
10 Correct 23 ms 23032 KB Output is correct
11 Correct 23 ms 23032 KB Output is correct
12 Correct 23 ms 23032 KB Output is correct
13 Correct 20 ms 23032 KB Output is correct
14 Correct 24 ms 23032 KB Output is correct
15 Correct 23 ms 23032 KB Output is correct
16 Correct 23 ms 23032 KB Output is correct
17 Correct 23 ms 23032 KB Output is correct
18 Correct 23 ms 23032 KB Output is correct
19 Correct 28 ms 23004 KB Output is correct
20 Correct 23 ms 23004 KB Output is correct
21 Correct 25 ms 23160 KB Output is correct
22 Correct 30 ms 23160 KB Output is correct
23 Correct 29 ms 23160 KB Output is correct
24 Correct 25 ms 23160 KB Output is correct
25 Correct 33 ms 23160 KB Output is correct
26 Correct 25 ms 23160 KB Output is correct
27 Correct 24 ms 23172 KB Output is correct
28 Correct 31 ms 23160 KB Output is correct
29 Correct 26 ms 23160 KB Output is correct
30 Correct 29 ms 23188 KB Output is correct
31 Correct 41 ms 23288 KB Output is correct
32 Correct 25 ms 23160 KB Output is correct
33 Correct 25 ms 23160 KB Output is correct
34 Correct 27 ms 23160 KB Output is correct
35 Correct 25 ms 23160 KB Output is correct
36 Correct 24 ms 23160 KB Output is correct
37 Correct 24 ms 23160 KB Output is correct
38 Correct 24 ms 23160 KB Output is correct
39 Correct 446 ms 34408 KB Output is correct
40 Correct 382 ms 34264 KB Output is correct
41 Correct 432 ms 35188 KB Output is correct
42 Correct 700 ms 36264 KB Output is correct
43 Correct 226 ms 34016 KB Output is correct
44 Correct 189 ms 33588 KB Output is correct
45 Correct 322 ms 36964 KB Output is correct
46 Correct 223 ms 40616 KB Output is correct
47 Correct 1444 ms 45764 KB Output is correct
48 Correct 640 ms 55416 KB Output is correct
49 Correct 661 ms 54900 KB Output is correct
50 Correct 1433 ms 45736 KB Output is correct
51 Correct 1414 ms 45816 KB Output is correct
52 Correct 1516 ms 45908 KB Output is correct
53 Correct 615 ms 45412 KB Output is correct
54 Correct 525 ms 46476 KB Output is correct
55 Correct 61 ms 24284 KB Output is correct
56 Correct 49 ms 24440 KB Output is correct
57 Correct 210 ms 36132 KB Output is correct
58 Execution timed out 3027 ms 34780 KB Time limit exceeded
59 Halted 0 ms 0 KB -