## Submission #387265

# Submission time Handle Problem Language Result Execution time Memory
387265 2021-04-08T08:02:08 Z milleniumEeee Fun Tour (APIO20_fun) C++17
47 / 100
762 ms 99300 KB
```////////////////////////////////
#include "fun.h"
////////////////////////////////
#include <bits/stdc++.h>
#define pb push_back
#define szof(s) (int)s.size()
#define all(s) s.begin(), s.end()
#define fr first
#define sc second
#define pii pair<int, int>
using namespace std;

const int MAXN = (int)1e5 + 5;
const int MAXQ = (int)4e5 + 5;

vector <int> g[MAXN];
bool used[MAXN];
int dist[MAXN];
int n;

void calc(int v, int par, int len) {
dist[v] = len;
for (int to : g[v]) {
if (to == par) {
continue;
}
calc(to, v, len + 1);
}
}

int get_max() {
int pos = -1, mx = 0;
for (int i = 0; i < n; i++) {
if (!used[i] && dist[i] > mx) {
mx = dist[i];
pos = i;
}
}
return pos;
}

multiset <pii> st[MAXN];
void precalc(int v, int par) {
for (int to : g[v]) {
if (to == par) {
continue;
}
precalc(to, v);
for (pii el : st[to]) {
st[v].insert({el.first + 1, el.second});
}
}
st[v].insert({0, v});
}

void ers(int v) {
int cur = v;
int len = 0;
while (1) {
st[cur].erase(st[cur].find({len, v}));
if (cur == 0) {
break;
}
len++;
cur = (cur - 1) / 2;
}
}

vector <int> createFunTour(int N, int Q) {
n = N;
if (N <= 500) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (hoursRequired(i, j) == 1) {
g[i].pb(j);
g[j].pb(i);
}
}
}
int a = 1;
calc(a, -1, 0);
int b = get_max();
calc(b, -1, 0);
int c = get_max();
vector <int> ans = {b, c};
used[b] = used[c] = 1;
int used_cnt = 2;
int last = c;
while (used_cnt < n) {
calc(last, -1, 0);
int nxt = get_max();
last = nxt;
ans.pb(nxt);
used[last] = 1;
used_cnt++;
}
return ans;
}
else { // binary tree
for (int i = 1; i < N; i++) {
int x = i;
int y = (i - 1) / 2;
g[x].pb(y);
g[y].pb(x);
}
precalc(0, -1);
int a = 0;
calc(a, -1, 0);
int b = get_max();
calc(b, -1, 0);
int c = get_max();
vector <int> ans = {b, c};
ers(b);
ers(c);
int last = c;

while (szof(ans) < n) {
vector <pii> vec;
int cur = last;
int len = 0;
while (1) {
vec.pb({len, cur});
if (cur == 0) {
break;
}
cur = (cur - 1) / 2;
len++;
}
//cout << "wtf: " << last << endl;
//cout << "st: " << endl;
//for (int i = 0; i < n; i++) {
//cout << i << ": ";
//for (auto el : st[i]) {
//cout << el.fr << " " << el.sc << "|";
//}cout << endl;
//}

//cout << "vec: " << endl;
//for (auto el : vec) {
//cout << el.sc << " ";
//}cout << endl;

int best = -1, mx = -1;
if (!st[last].empty()) {
pii joop = *(--st[last].end());
best = joop.sc;
mx = joop.fr;
}

auto exist = [&](vector <pii> &vec, int vertex) {
for (auto el : vec) {
if (el.sc == vertex) {
return true;
}
}
return false;
};

//cout << "here" << endl;

for (auto el : vec) {
int v = el.sc;
int len = el.fr;
if (v == last) {
continue;
}
// parents
for (int to : g[v]) {
if (!exist(vec, to) && !st[to].empty()) {
pii joop = *(--st[to].end());
if (joop.fr + len > mx) {
mx = joop.fr + len;
best = joop.sc;
}
}
}
if (st[v].find({0, v}) != st[v].end()) {
if (len > mx) {
mx = len;
best = v;
}
}
}
ans.pb(best);
ers(best);
last = best;
}

//cout << "ans : " << endl;
//for (int el : ans) {
//cout << el << " ";
//}cout << endl;

return ans;
}
}

/*
7 400000
1 0
2 0
3 1
4 1
5 2
6 2
*/
```

#### Subtask #1 10.0 / 10.0

# Verdict Execution time Memory Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 5 ms 7404 KB Output is correct
7 Correct 5 ms 7404 KB Output is correct
8 Correct 5 ms 7404 KB Output is correct
9 Correct 5 ms 7404 KB Output is correct
10 Correct 5 ms 7404 KB Output is correct
11 Correct 6 ms 7404 KB Output is correct
12 Correct 5 ms 7404 KB Output is correct
13 Correct 6 ms 7404 KB Output is correct
14 Correct 5 ms 7404 KB Output is correct
15 Correct 6 ms 7404 KB Output is correct
16 Correct 6 ms 7404 KB Output is correct
17 Correct 6 ms 7404 KB Output is correct
18 Correct 6 ms 7424 KB Output is correct
19 Correct 6 ms 7404 KB Output is correct
20 Correct 5 ms 7532 KB Output is correct

#### Subtask #2 16.0 / 16.0

# Verdict Execution time Memory Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 5 ms 7404 KB Output is correct
7 Correct 5 ms 7404 KB Output is correct
8 Correct 5 ms 7404 KB Output is correct
9 Correct 5 ms 7404 KB Output is correct
10 Correct 5 ms 7404 KB Output is correct
11 Correct 6 ms 7404 KB Output is correct
12 Correct 5 ms 7404 KB Output is correct
13 Correct 6 ms 7404 KB Output is correct
14 Correct 5 ms 7404 KB Output is correct
15 Correct 6 ms 7404 KB Output is correct
16 Correct 6 ms 7404 KB Output is correct
17 Correct 6 ms 7404 KB Output is correct
18 Correct 6 ms 7424 KB Output is correct
19 Correct 6 ms 7404 KB Output is correct
20 Correct 5 ms 7532 KB Output is correct
21 Correct 5 ms 7404 KB Output is correct
22 Correct 5 ms 7404 KB Output is correct
23 Correct 6 ms 7404 KB Output is correct
24 Correct 23 ms 7404 KB Output is correct
25 Correct 6 ms 7404 KB Output is correct
26 Correct 6 ms 7404 KB Output is correct
27 Correct 7 ms 7404 KB Output is correct
28 Correct 5 ms 7404 KB Output is correct
29 Correct 19 ms 7532 KB Output is correct
30 Correct 6 ms 7404 KB Output is correct
31 Correct 7 ms 7404 KB Output is correct
32 Correct 9 ms 7404 KB Output is correct
33 Correct 6 ms 7404 KB Output is correct
34 Correct 19 ms 7404 KB Output is correct
35 Correct 19 ms 7424 KB Output is correct
36 Correct 21 ms 7404 KB Output is correct
37 Correct 6 ms 7404 KB Output is correct
38 Correct 19 ms 7424 KB Output is correct
39 Correct 9 ms 7404 KB Output is correct
40 Correct 12 ms 7404 KB Output is correct
41 Correct 8 ms 7404 KB Output is correct

#### Subtask #3 21.0 / 21.0

# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 5 ms 7404 KB Output is correct
7 Correct 5 ms 7404 KB Output is correct
8 Correct 5 ms 7404 KB Output is correct
9 Correct 5 ms 7404 KB Output is correct
10 Correct 5 ms 7404 KB Output is correct
11 Correct 6 ms 7404 KB Output is correct
12 Correct 23 ms 7404 KB Output is correct
13 Correct 6 ms 7404 KB Output is correct
14 Correct 6 ms 7404 KB Output is correct
15 Correct 7 ms 7404 KB Output is correct
16 Correct 7 ms 7660 KB Output is correct
17 Correct 7 ms 7660 KB Output is correct
18 Correct 762 ms 99300 KB Output is correct
19 Correct 8 ms 7916 KB Output is correct
20 Correct 12 ms 8556 KB Output is correct
21 Correct 17 ms 9324 KB Output is correct
22 Correct 37 ms 12268 KB Output is correct
23 Correct 77 ms 17772 KB Output is correct
24 Correct 107 ms 21740 KB Output is correct
25 Correct 413 ms 58756 KB Output is correct

#### Subtask #4 0 / 19.0

# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 6 ms 7404 KB Output is correct
7 Correct 5 ms 7404 KB Output is correct
8 Correct 19 ms 7532 KB Output is correct
9 Correct 6 ms 7404 KB Output is correct
10 Correct 7 ms 7404 KB Output is correct
11 Correct 9 ms 7404 KB Output is correct
12 Correct 6 ms 7404 KB Output is correct
13 Incorrect 8 ms 7916 KB Tour is not fun
14 Halted 0 ms 0 KB -

#### Subtask #5 0 / 34.0

# Verdict Execution time Memory Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 5 ms 7404 KB Output is correct
7 Correct 5 ms 7404 KB Output is correct
8 Correct 5 ms 7404 KB Output is correct
9 Correct 5 ms 7404 KB Output is correct
10 Correct 5 ms 7404 KB Output is correct
11 Correct 6 ms 7404 KB Output is correct
12 Correct 5 ms 7404 KB Output is correct
13 Correct 6 ms 7404 KB Output is correct
14 Correct 5 ms 7404 KB Output is correct
15 Correct 6 ms 7404 KB Output is correct
16 Correct 6 ms 7404 KB Output is correct
17 Correct 6 ms 7404 KB Output is correct
18 Correct 6 ms 7424 KB Output is correct
19 Correct 6 ms 7404 KB Output is correct
20 Correct 5 ms 7532 KB Output is correct
21 Correct 5 ms 7404 KB Output is correct
22 Correct 5 ms 7404 KB Output is correct
23 Correct 6 ms 7404 KB Output is correct
24 Correct 23 ms 7404 KB Output is correct
25 Correct 6 ms 7404 KB Output is correct
26 Correct 6 ms 7404 KB Output is correct
27 Correct 7 ms 7404 KB Output is correct
28 Correct 5 ms 7404 KB Output is correct
29 Correct 19 ms 7532 KB Output is correct
30 Correct 6 ms 7404 KB Output is correct
31 Correct 7 ms 7404 KB Output is correct
32 Correct 9 ms 7404 KB Output is correct
33 Correct 6 ms 7404 KB Output is correct
34 Correct 19 ms 7404 KB Output is correct
35 Correct 19 ms 7424 KB Output is correct
36 Correct 21 ms 7404 KB Output is correct
37 Correct 6 ms 7404 KB Output is correct
38 Correct 19 ms 7424 KB Output is correct
39 Correct 9 ms 7404 KB Output is correct
40 Correct 12 ms 7404 KB Output is correct
41 Correct 8 ms 7404 KB Output is correct
42 Correct 7 ms 7660 KB Output is correct
43 Correct 7 ms 7660 KB Output is correct
44 Correct 762 ms 99300 KB Output is correct
45 Correct 8 ms 7916 KB Output is correct
46 Correct 12 ms 8556 KB Output is correct
47 Correct 17 ms 9324 KB Output is correct
48 Correct 37 ms 12268 KB Output is correct
49 Correct 77 ms 17772 KB Output is correct
50 Correct 107 ms 21740 KB Output is correct
51 Correct 413 ms 58756 KB Output is correct
52 Incorrect 8 ms 7916 KB Tour is not fun
53 Halted 0 ms 0 KB -