Submission #854881

# Submission time Handle Problem Language Result Execution time Memory
854881 2023-09-29T08:55:08 Z piOOE Beech Tree (IOI23_beechtree) C++17
49 / 100
2000 ms 1629588 KB
#include "beechtree.h"
#include <bits/stdc++.h>

using namespace std;

std::vector<int> beechtree(int N, int M, std::vector<int> P, std::vector<int> C) {
    vector<int> sh(C);
    sort(sh.begin(), sh.end());
    sh.resize(unique(sh.begin(), sh.end()) - sh.begin());
    M = size(sh);
    for (int i = 0; i < N; ++i) {
        C[i] = lower_bound(sh.begin(), sh.end(), C[i]) - sh.begin();
    }

    vector<int> ans(N, 1), isOk(N, 1), siz(N, 1);
    vector<vector<int>> adj(N), sub(N);

    for (int i = N - 1; i > 0; --i) {
        adj[P[i]].push_back(i);
    }

    for (int v = N - 1; v >= 0; --v) {
        static vector<int> cnt(M);
        for (int to : adj[v]) {
            cnt[C[to]] = 0;
            isOk[v] &= isOk[to];
        }
        for (int to : adj[v]) {
            siz[v] += siz[to];
            isOk[v] &= ++cnt[C[to]] == 1;
        }
        ans[v] = isOk[v];
        if (!isOk[v]) {
            continue;
        }
        for (int to : adj[v]) {
            sub[v].insert(sub[v].end(), sub[to].begin(), sub[to].end());
            sub[to].clear();
        }
        static vector<int> last(N, -1);
        last[v] = -1;
        for (int x : sub[v]) {
            last[x] = -1;
            ++cnt[C[x]];
        }
//        vector<int> ord = sub[v];
//        sort(ord.begin(), ord.end(), [&](int x, int y) { return pair{cnt[C[x]], C[x]} > pair{cnt[C[y]], C[y]}; });
//        for (int i = 0, j = 0, k = -1; i < size(ord); i = j) {
//            int sum = 0;
//            while (j < size(ord) && C[ord[j]] == C[ord[i]]) {
//                int x = ord[j++];
//                while (x != v) {
//                    x = P[x];
//                    if (last[x] == i) {
//                        break;
//                    } else if (last[x] != k) {
//                        ans[v] = 0;
//                        break;
//                    }
//                    sum += 1;
//                    last[x] = i;
//                }
//                if (!ans[v]) {
//                    break;
//                }
//            }
//            if (!ans[v] || sum != j - i) {
//                ans[v] = 0;
//                break;
//            }
//            k = i;
//        }
//        if (!ans[v]) {
//            sub[v].push_back(v);
//            continue;
//        }
        vector<int> q;
        set<array<int, 3>> st;
        vector<int> tin(N);
        st.insert({siz[v], tin[v] = 0, v});
        int T = 0;
        for (int i = 0; !st.empty() && ans[v]; ++i) {
            q.push_back(st.rbegin()->begin()[2]);
            st.erase(--st.end());
            int x = q[i];
            if (i > 0) {
                int c = 0;
                for (int j = 1; j < i; ++j) {
                    c += C[q[j]] == C[x];
                }
                if (P[x] != q[c]) {
                    ans[v] = 0;
                    break;
                }
            }
            sort(adj[x].begin(), adj[x].end(), [&](int a, int b) { return siz[a] > siz[b]; });
            for (int to : adj[x]) {
                st.insert({siz[to], tin[to] = --T, to});
            }
        }
        sub[v].push_back(v);
    }

    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 344 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 352 KB Output is correct
34 Correct 0 ms 352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Execution timed out 2149 ms 1629588 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 2 ms 860 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 2 ms 600 KB Output is correct
15 Correct 875 ms 20792 KB Output is correct
16 Correct 514 ms 19236 KB Output is correct
17 Correct 607 ms 19700 KB Output is correct
18 Execution timed out 2032 ms 20296 KB Time limit exceeded
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Execution timed out 2149 ms 1629588 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 352 KB Output is correct
37 Correct 0 ms 352 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 3 ms 604 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 1 ms 604 KB Output is correct
51 Correct 1 ms 604 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Correct 0 ms 348 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 1 ms 348 KB Output is correct
58 Correct 0 ms 344 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 1 ms 352 KB Output is correct
61 Correct 1 ms 344 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1156 ms 14680 KB Output is correct
26 Correct 498 ms 14480 KB Output is correct
27 Correct 245 ms 14316 KB Output is correct
28 Correct 17 ms 14680 KB Output is correct
29 Correct 1144 ms 14528 KB Output is correct
30 Correct 20 ms 1884 KB Output is correct
31 Correct 8 ms 900 KB Output is correct
32 Correct 4 ms 604 KB Output is correct
33 Correct 3 ms 604 KB Output is correct
34 Correct 8 ms 860 KB Output is correct
35 Correct 4 ms 604 KB Output is correct
36 Correct 2 ms 600 KB Output is correct
37 Correct 187 ms 4476 KB Output is correct
38 Correct 25 ms 2112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 352 KB Output is correct
37 Correct 0 ms 352 KB Output is correct
38 Correct 3 ms 604 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 344 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 1 ms 604 KB Output is correct
53 Correct 2 ms 860 KB Output is correct
54 Correct 1 ms 604 KB Output is correct
55 Correct 2 ms 600 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 0 ms 348 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 1 ms 348 KB Output is correct
62 Correct 0 ms 344 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 1 ms 352 KB Output is correct
65 Correct 1 ms 344 KB Output is correct
66 Correct 0 ms 348 KB Output is correct
67 Correct 1 ms 348 KB Output is correct
68 Correct 1 ms 348 KB Output is correct
69 Correct 1156 ms 14680 KB Output is correct
70 Correct 498 ms 14480 KB Output is correct
71 Correct 245 ms 14316 KB Output is correct
72 Correct 17 ms 14680 KB Output is correct
73 Correct 1144 ms 14528 KB Output is correct
74 Correct 20 ms 1884 KB Output is correct
75 Correct 8 ms 900 KB Output is correct
76 Correct 4 ms 604 KB Output is correct
77 Correct 3 ms 604 KB Output is correct
78 Correct 8 ms 860 KB Output is correct
79 Correct 4 ms 604 KB Output is correct
80 Correct 2 ms 600 KB Output is correct
81 Correct 187 ms 4476 KB Output is correct
82 Correct 25 ms 2112 KB Output is correct
83 Correct 1143 ms 14656 KB Output is correct
84 Correct 479 ms 14384 KB Output is correct
85 Correct 20 ms 14344 KB Output is correct
86 Correct 14 ms 14424 KB Output is correct
87 Correct 2 ms 604 KB Output is correct
88 Correct 2 ms 604 KB Output is correct
89 Correct 188 ms 6056 KB Output is correct
90 Correct 11 ms 1372 KB Output is correct
91 Correct 7 ms 600 KB Output is correct
92 Correct 3 ms 604 KB Output is correct
93 Correct 2 ms 600 KB Output is correct
94 Correct 5 ms 600 KB Output is correct
95 Correct 3 ms 600 KB Output is correct
96 Correct 2 ms 604 KB Output is correct
97 Correct 186 ms 4544 KB Output is correct
98 Correct 24 ms 1956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1156 ms 14680 KB Output is correct
26 Correct 498 ms 14480 KB Output is correct
27 Correct 245 ms 14316 KB Output is correct
28 Correct 17 ms 14680 KB Output is correct
29 Correct 1144 ms 14528 KB Output is correct
30 Correct 20 ms 1884 KB Output is correct
31 Correct 8 ms 900 KB Output is correct
32 Correct 4 ms 604 KB Output is correct
33 Correct 3 ms 604 KB Output is correct
34 Correct 8 ms 860 KB Output is correct
35 Correct 4 ms 604 KB Output is correct
36 Correct 2 ms 600 KB Output is correct
37 Correct 187 ms 4476 KB Output is correct
38 Correct 25 ms 2112 KB Output is correct
39 Execution timed out 2058 ms 42780 KB Time limit exceeded
40 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 352 KB Output is correct
37 Correct 0 ms 352 KB Output is correct
38 Execution timed out 2149 ms 1629588 KB Time limit exceeded
39 Halted 0 ms 0 KB -