Submission #347076

# Submission time Handle Problem Language Result Execution time Memory
347076 2021-01-11T16:53:50 Z patrikpavic2 Fun Tour (APIO20_fun) C++17
100 / 100
426 ms 26084 KB
#include "fun.h"
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <set>

#define X first
#define Y second
#define PB push_back

using namespace std;

typedef vector < int > vi;
typedef pair < int, int > pii;

const int N = 1e5 + 500;

int root, siz[N], dep[N], ty[N], n;
vector < int > ch;
set < pii > mj[3];

void nadi_centroid(){
	int bst = 0; siz[0] = n;
	for(int i = 1;i < n;i++){
		siz[i] = attractionsBehind(root, i);
		if(2 * siz[i] >= n && siz[i] < siz[bst])
			bst = i;
	}
	root = bst;
}


void nadi_udaljenost(){
	for(int i = 0;i < n;i++){
		if(i == root){
			ty[i] = -1;
			dep[i] = 0; continue;
		}
		dep[i] = hoursRequired(root, i);
		if(dep[i] == 1){
			ty[i] = (int)ch.size();
			ch.PB(i);
		}
	}
}

void nadi_skupinu(){
	for(int i = 0;i < n;i++){
		if(i == root || dep[i] == 1) continue;
		while(ty[i] + 1 < (int)ch.size() && dep[i] - 1 != hoursRequired(i, ch[ty[i]]))
			ty[i]++;
	}
}

vi perm2(){
	mj[0].clear(); mj[1].clear(); mj[2].clear();
	for(int i = 0;i < n;i++){
		//printf("%d %d %d\n", i, ty[i], dep[i]); 
		if(!dep[i]) continue;
		mj[ty[i]].insert({dep[i], i});
	}
	int zad = -1;
	vi ans;
	for(int i = 1;i < n;i++){
		int bst = -1, krit = max(mj[0].size(), max(mj[1].size(), mj[2].size()));
		for(int j = 0;j < (int)ch.size();j++){
			if(j == zad) continue;
			if(2 * krit >= n - i && (int)mj[j].size() != krit) continue;
			if(!mj[j].size()) continue;
			if(bst == -1 || (mj[j].rbegin() -> X) > dep[bst]){
				bst = mj[j].rbegin() -> Y;
			}
		}
		ans.PB(bst);
		mj[ty[bst]].erase({dep[bst], bst});
		zad = ty[bst];
	}
	ans.PB(root);
	//printf("%d\n", root);
	return ans;
}

// MAGIJA PREVARE

vi perm(){
	for(int i = 0;i < n;i++){
		//printf("%d %d %d\n", i, ty[i], dep[i]); 
		if(!dep[i]) continue;
		mj[ty[i]].insert({dep[i], i});
	}
	int zad = -1;
	vi ans;
	for(int i = 1;i < n;i++){
		int bst = -1, krit = max(mj[0].size(), max(mj[1].size(), mj[2].size()));
		//printf("VELICINE %d %d %d\n", (int)mj[0].size(), (int)mj[1].size(), (int)mj[2].size());
		//printf("krit = %d ost = %d\n", krit, n - i);
		//if(2 * krit - 1 >= n - i)
		//	printf("KRITICNO!!!\n");
		for(int j = 0;j < (int)ch.size();j++){
			if(j == zad) continue;
			if(2 * krit - 1 >= n - i && (int)mj[j].size() != krit) continue;
			if(!mj[j].size()) continue;
			if(bst == -1 || (mj[j].rbegin() -> X) > dep[bst]){
				bst = mj[j].rbegin() -> Y;
			}
		}
		//printf("				%d %d %d\n", bst, dep[bst], ty[bst]);
		if(i > 2 && dep[ans[ans.size() - 2]] < dep[bst]){
			return perm2();
			
		}
		ans.PB(bst);
		mj[ty[bst]].erase({dep[bst], bst});
		zad = ty[bst];
	}
	ans.PB(root);
	//printf("%d\n", root);
	return ans;
}




vi createFunTour(int nn, int q) {
	n = nn; root = 0;
	nadi_centroid();
	nadi_udaljenost();
	nadi_skupinu();
	return perm();	
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 0 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 0 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 2 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 1 ms 364 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 1 ms 364 KB Output is correct
41 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 168 ms 20136 KB Output is correct
19 Correct 2 ms 492 KB Output is correct
20 Correct 3 ms 620 KB Output is correct
21 Correct 5 ms 876 KB Output is correct
22 Correct 8 ms 1516 KB Output is correct
23 Correct 16 ms 2924 KB Output is correct
24 Correct 22 ms 3692 KB Output is correct
25 Correct 91 ms 11780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
14 Correct 14 ms 2284 KB Output is correct
15 Correct 176 ms 21092 KB Output is correct
16 Correct 196 ms 20840 KB Output is correct
17 Correct 39 ms 5356 KB Output is correct
18 Correct 76 ms 9780 KB Output is correct
19 Correct 129 ms 16444 KB Output is correct
20 Correct 6 ms 1004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 0 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 2 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 1 ms 364 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 1 ms 364 KB Output is correct
41 Correct 1 ms 364 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 168 ms 20136 KB Output is correct
45 Correct 2 ms 492 KB Output is correct
46 Correct 3 ms 620 KB Output is correct
47 Correct 5 ms 876 KB Output is correct
48 Correct 8 ms 1516 KB Output is correct
49 Correct 16 ms 2924 KB Output is correct
50 Correct 22 ms 3692 KB Output is correct
51 Correct 91 ms 11780 KB Output is correct
52 Correct 2 ms 492 KB Output is correct
53 Correct 14 ms 2284 KB Output is correct
54 Correct 176 ms 21092 KB Output is correct
55 Correct 196 ms 20840 KB Output is correct
56 Correct 39 ms 5356 KB Output is correct
57 Correct 76 ms 9780 KB Output is correct
58 Correct 129 ms 16444 KB Output is correct
59 Correct 6 ms 1004 KB Output is correct
60 Correct 226 ms 21220 KB Output is correct
61 Correct 290 ms 23556 KB Output is correct
62 Correct 284 ms 21220 KB Output is correct
63 Correct 255 ms 25188 KB Output is correct
64 Correct 323 ms 25060 KB Output is correct
65 Correct 271 ms 21220 KB Output is correct
66 Correct 381 ms 23524 KB Output is correct
67 Correct 378 ms 25044 KB Output is correct
68 Correct 348 ms 24292 KB Output is correct
69 Correct 406 ms 25852 KB Output is correct
70 Correct 235 ms 21220 KB Output is correct
71 Correct 309 ms 25168 KB Output is correct
72 Correct 394 ms 24112 KB Output is correct
73 Correct 348 ms 24420 KB Output is correct
74 Correct 393 ms 26084 KB Output is correct
75 Correct 304 ms 21220 KB Output is correct
76 Correct 286 ms 25200 KB Output is correct
77 Correct 323 ms 25124 KB Output is correct
78 Correct 326 ms 24352 KB Output is correct
79 Correct 426 ms 25768 KB Output is correct
80 Correct 425 ms 26052 KB Output is correct
81 Correct 252 ms 21368 KB Output is correct
82 Correct 327 ms 21568 KB Output is correct
83 Correct 285 ms 21332 KB Output is correct
84 Correct 109 ms 9156 KB Output is correct