Submission #945207

# Submission time Handle Problem Language Result Execution time Memory
945207 2024-03-13T14:16:38 Z Nhoksocqt1 Beech Tree (IOI23_beechtree) C++17
18 / 100
2000 ms 41312 KB
#ifndef Nhoksocqt1
    #include "beechtree.h"
#endif // Nhoksocqt1
#include<bits/stdc++.h>
using namespace std;

#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;

template<class X, class Y>
	inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
	inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

const bool isMultiTest = 0;
const int MAXN = 200005;

vector<int> adj[MAXN], B[MAXN];
int maxDepth[MAXN];
int depth[MAXN], par[MAXN], col[MAXN], cnt[MAXN], lastQuery[MAXN], numNode, numColor;
bool canChoose[MAXN];

void preDfs(int u) {
    canChoose[u] = 1;
    for (int it = 0; it < sz(adj[u]); ++it) {
        int v(adj[u][it]);
        if(lastQuery[col[v]])
            canChoose[u] = 0;

        lastQuery[col[v]] = 1;
    }

    for (int it = 0; it < sz(adj[u]); ++it) {
        int v(adj[u][it]);
        lastQuery[col[v]] = 0;
    }

    maxDepth[u] = depth[u];
    for (int it = 0; it < sz(adj[u]); ++it) {
        int v(adj[u][it]);
        depth[v] = depth[u] + 1;

        preDfs(v);
        maxDepth[u] = max(maxDepth[u], maxDepth[v]);
        canChoose[u] &= canChoose[v];
    }
}

void dfs1(int u) {
    B[u].clear();
    B[u].push_back(u);
    for (int it = 0; it < sz(adj[u]); ++it) {
        int v(adj[u][it]);
        dfs1(v);
        for (int jt = 0; jt < sz(B[v]); ++jt)
            B[u].push_back(B[v][jt]);
    }
}

vector<int> sub1(void) {
    dfs1(0);

    vector<int> ans;
    for (int i = 0; i < numNode; ++i) {
        bool check(0);
        do {
            for (int it = 0; it < sz(B[i]); ++it)
                cnt[col[B[i][it]]] = 0;

            check = 1;
            for (int it = 1; it < sz(B[i]); ++it) {
                int u(B[i][it]);
                if(B[i][cnt[col[u]]] != par[u]) {
                    check = 0;
                    break;
                }

                ++cnt[col[u]];
            }

            if(check) {
                /*cout << i << ": ";
                for (int it = 0; it < sz(B[i]); ++it)
                    cout << B[i][it] << ' '; cout << '\n';*/
                break;
            }

        } while(next_permutation(B[i].begin() + 1, B[i].end()));
        ans.push_back(check);
    }

    return ans;
}

vector<int> subn2(void) {
    vector<int> ans;
    for (int i = 0; i < numNode; ++i) {
        if(!canChoose[i]) {
            ans.push_back(0);
            continue;
        }

        if(maxDepth[i] - depth[i] <= 1) {
            ans.push_back(1);
            continue;
        }

        for (int j = 0; j <= numNode; ++j) {
            B[j].clear();
            lastQuery[col[j]] = 0;
        }

        B[sz(adj[i])].push_back(i);

        int cnt(0);
        bool check(1);
        for (int j = numNode; j > 0; --j) {
            for (int it = 0; it < sz(B[j]); ++it) {
                int u(B[j][it]);
                for (int jt = 0; jt < sz(adj[u]); ++jt) {
                    int v(adj[u][jt]);
                    if(lastQuery[col[v]] != cnt || sz(adj[v]) > j) {
                        check = 0;
                        break;
                    }

                    lastQuery[col[v]] = cnt + 1;
                    B[sz(adj[v])].push_back(v);
                }

                ++cnt;
                if(!check)
                    break;
            }

            if(!check)
                break;
        }

        ans.push_back(check);
    }

    //for (int it = 0; it < sz(ans); ++it) cout << ans[it]; cout << '\n';
    return ans;
}

vector<int> beechtree(int _N, int _M, vector<int> _P, vector<int> _C) {
    numNode = _N, numColor = _M;
    for (int i = 0; i < numNode; ++i) {
        par[i] = _P[i];
        col[i] = _C[i];
        if(i > 0)
            adj[par[i]].push_back(i);
    }

    preDfs(0);
    if(numNode <= 8) {
        return sub1();
    } else
        if(numNode <= 2000) {
            return subn2();
        }
}

#ifdef Nhoksocqt1

int main(void) {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    #define TASK "beechtree"
    if(fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }

    vector<int> P, C;
    int N, M;
    cin >> N >> M;

    P.resize(N);
    C.resize(N);
    for (int i = 0; i < N; ++i)
        cin >> P[i];

    for (int i = 0; i < N; ++i)
        cin >> C[i];

    vector<int> ans = beechtree(N, M, P, C);
    cout << "ANSWER: ";
    for (int it = 0; it < sz(ans); ++it)
        cout << ans[it]; cout << '\n';

    return 0;
}

#endif // Nhoksocqt1

Compilation message

beechtree.cpp: In function 'std::vector<int> beechtree(int, int, std::vector<int>, std::vector<int>)':
beechtree.cpp:172:1: warning: control reaches end of non-void function [-Wreturn-type]
  172 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14424 KB Output is correct
2 Correct 3 ms 14428 KB Output is correct
3 Correct 3 ms 14428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14428 KB Output is correct
2 Correct 3 ms 14428 KB Output is correct
3 Correct 3 ms 14428 KB Output is correct
4 Correct 3 ms 14428 KB Output is correct
5 Correct 3 ms 14428 KB Output is correct
6 Correct 4 ms 14544 KB Output is correct
7 Correct 4 ms 14428 KB Output is correct
8 Correct 3 ms 14428 KB Output is correct
9 Correct 3 ms 14424 KB Output is correct
10 Correct 3 ms 14428 KB Output is correct
11 Correct 3 ms 14544 KB Output is correct
12 Correct 3 ms 14428 KB Output is correct
13 Correct 3 ms 14428 KB Output is correct
14 Correct 3 ms 14428 KB Output is correct
15 Correct 3 ms 14428 KB Output is correct
16 Correct 3 ms 14424 KB Output is correct
17 Correct 4 ms 14428 KB Output is correct
18 Correct 3 ms 14424 KB Output is correct
19 Correct 3 ms 14428 KB Output is correct
20 Correct 3 ms 14428 KB Output is correct
21 Correct 3 ms 14428 KB Output is correct
22 Correct 4 ms 14428 KB Output is correct
23 Correct 3 ms 14424 KB Output is correct
24 Correct 3 ms 14428 KB Output is correct
25 Correct 3 ms 14428 KB Output is correct
26 Correct 3 ms 14428 KB Output is correct
27 Correct 3 ms 14428 KB Output is correct
28 Correct 3 ms 14428 KB Output is correct
29 Correct 3 ms 14428 KB Output is correct
30 Correct 3 ms 14624 KB Output is correct
31 Correct 4 ms 14548 KB Output is correct
32 Correct 3 ms 14424 KB Output is correct
33 Correct 3 ms 14424 KB Output is correct
34 Correct 3 ms 14428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14428 KB Output is correct
2 Correct 3 ms 14428 KB Output is correct
3 Correct 3 ms 14428 KB Output is correct
4 Correct 3 ms 14428 KB Output is correct
5 Correct 3 ms 14428 KB Output is correct
6 Correct 4 ms 14544 KB Output is correct
7 Execution timed out 2059 ms 41312 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14428 KB Output is correct
2 Correct 3 ms 14544 KB Output is correct
3 Correct 3 ms 14428 KB Output is correct
4 Correct 3 ms 14428 KB Output is correct
5 Correct 4 ms 14428 KB Output is correct
6 Correct 4 ms 14428 KB Output is correct
7 Correct 4 ms 14428 KB Output is correct
8 Correct 3 ms 14428 KB Output is correct
9 Correct 3 ms 14428 KB Output is correct
10 Correct 3 ms 14428 KB Output is correct
11 Correct 3 ms 14428 KB Output is correct
12 Correct 4 ms 14428 KB Output is correct
13 Correct 3 ms 14428 KB Output is correct
14 Correct 3 ms 14428 KB Output is correct
15 Correct 30 ms 18232 KB Output is correct
16 Correct 28 ms 18136 KB Output is correct
17 Correct 28 ms 18128 KB Output is correct
18 Correct 29 ms 18892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14428 KB Output is correct
2 Correct 3 ms 14428 KB Output is correct
3 Correct 4 ms 14428 KB Output is correct
4 Correct 3 ms 14428 KB Output is correct
5 Execution timed out 2059 ms 41312 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14424 KB Output is correct
2 Correct 3 ms 14428 KB Output is correct
3 Correct 3 ms 14428 KB Output is correct
4 Correct 3 ms 14428 KB Output is correct
5 Correct 3 ms 14428 KB Output is correct
6 Correct 3 ms 14428 KB Output is correct
7 Correct 3 ms 14428 KB Output is correct
8 Correct 3 ms 14428 KB Output is correct
9 Correct 4 ms 14544 KB Output is correct
10 Correct 4 ms 14428 KB Output is correct
11 Correct 3 ms 14428 KB Output is correct
12 Correct 3 ms 14424 KB Output is correct
13 Correct 3 ms 14428 KB Output is correct
14 Correct 3 ms 14544 KB Output is correct
15 Correct 3 ms 14428 KB Output is correct
16 Correct 3 ms 14428 KB Output is correct
17 Correct 3 ms 14428 KB Output is correct
18 Correct 3 ms 14428 KB Output is correct
19 Correct 3 ms 14424 KB Output is correct
20 Correct 4 ms 14428 KB Output is correct
21 Correct 3 ms 14424 KB Output is correct
22 Correct 3 ms 14428 KB Output is correct
23 Correct 3 ms 14428 KB Output is correct
24 Correct 3 ms 14428 KB Output is correct
25 Correct 4 ms 14428 KB Output is correct
26 Correct 3 ms 14424 KB Output is correct
27 Correct 3 ms 14428 KB Output is correct
28 Correct 3 ms 14428 KB Output is correct
29 Correct 3 ms 14428 KB Output is correct
30 Correct 3 ms 14428 KB Output is correct
31 Correct 3 ms 14428 KB Output is correct
32 Correct 3 ms 14428 KB Output is correct
33 Correct 3 ms 14624 KB Output is correct
34 Correct 4 ms 14548 KB Output is correct
35 Correct 3 ms 14424 KB Output is correct
36 Correct 3 ms 14424 KB Output is correct
37 Correct 3 ms 14428 KB Output is correct
38 Correct 3 ms 14428 KB Output is correct
39 Correct 3 ms 14544 KB Output is correct
40 Correct 3 ms 14428 KB Output is correct
41 Correct 3 ms 14428 KB Output is correct
42 Correct 4 ms 14428 KB Output is correct
43 Correct 4 ms 14428 KB Output is correct
44 Correct 4 ms 14428 KB Output is correct
45 Correct 3 ms 14428 KB Output is correct
46 Correct 3 ms 14428 KB Output is correct
47 Correct 3 ms 14428 KB Output is correct
48 Correct 4 ms 14428 KB Output is correct
49 Correct 4 ms 14472 KB Output is correct
50 Correct 5 ms 14428 KB Output is correct
51 Correct 4 ms 14548 KB Output is correct
52 Correct 3 ms 14428 KB Output is correct
53 Correct 3 ms 14428 KB Output is correct
54 Correct 3 ms 14428 KB Output is correct
55 Incorrect 3 ms 14428 KB 2nd lines differ - on the 82nd token, expected: '0', found: '1'
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14428 KB Output is correct
2 Correct 3 ms 14428 KB Output is correct
3 Correct 4 ms 14428 KB Output is correct
4 Correct 3 ms 14428 KB Output is correct
5 Correct 3 ms 14424 KB Output is correct
6 Correct 3 ms 14428 KB Output is correct
7 Correct 3 ms 14544 KB Output is correct
8 Correct 3 ms 14428 KB Output is correct
9 Correct 3 ms 14428 KB Output is correct
10 Correct 3 ms 14428 KB Output is correct
11 Correct 3 ms 14428 KB Output is correct
12 Correct 3 ms 14424 KB Output is correct
13 Correct 4 ms 14428 KB Output is correct
14 Correct 3 ms 14424 KB Output is correct
15 Correct 3 ms 14428 KB Output is correct
16 Correct 3 ms 14428 KB Output is correct
17 Correct 3 ms 14428 KB Output is correct
18 Correct 4 ms 14428 KB Output is correct
19 Correct 3 ms 14424 KB Output is correct
20 Correct 3 ms 14428 KB Output is correct
21 Correct 3 ms 14428 KB Output is correct
22 Correct 3 ms 14428 KB Output is correct
23 Correct 3 ms 14428 KB Output is correct
24 Correct 3 ms 14428 KB Output is correct
25 Correct 38 ms 14684 KB Output is correct
26 Correct 28 ms 14680 KB Output is correct
27 Correct 22 ms 14696 KB Output is correct
28 Correct 12 ms 14812 KB Output is correct
29 Correct 38 ms 14684 KB Output is correct
30 Correct 13 ms 14684 KB Output is correct
31 Correct 8 ms 14684 KB Output is correct
32 Incorrect 7 ms 14424 KB 2nd lines differ - on the 802nd token, expected: '0', found: '1'
33 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14424 KB Output is correct
2 Correct 3 ms 14428 KB Output is correct
3 Correct 3 ms 14428 KB Output is correct
4 Correct 3 ms 14428 KB Output is correct
5 Correct 3 ms 14428 KB Output is correct
6 Correct 3 ms 14428 KB Output is correct
7 Correct 3 ms 14428 KB Output is correct
8 Correct 3 ms 14428 KB Output is correct
9 Correct 4 ms 14544 KB Output is correct
10 Correct 4 ms 14428 KB Output is correct
11 Correct 3 ms 14428 KB Output is correct
12 Correct 3 ms 14424 KB Output is correct
13 Correct 3 ms 14428 KB Output is correct
14 Correct 3 ms 14544 KB Output is correct
15 Correct 3 ms 14428 KB Output is correct
16 Correct 3 ms 14428 KB Output is correct
17 Correct 3 ms 14428 KB Output is correct
18 Correct 3 ms 14428 KB Output is correct
19 Correct 3 ms 14424 KB Output is correct
20 Correct 4 ms 14428 KB Output is correct
21 Correct 3 ms 14424 KB Output is correct
22 Correct 3 ms 14428 KB Output is correct
23 Correct 3 ms 14428 KB Output is correct
24 Correct 3 ms 14428 KB Output is correct
25 Correct 4 ms 14428 KB Output is correct
26 Correct 3 ms 14424 KB Output is correct
27 Correct 3 ms 14428 KB Output is correct
28 Correct 3 ms 14428 KB Output is correct
29 Correct 3 ms 14428 KB Output is correct
30 Correct 3 ms 14428 KB Output is correct
31 Correct 3 ms 14428 KB Output is correct
32 Correct 3 ms 14428 KB Output is correct
33 Correct 3 ms 14624 KB Output is correct
34 Correct 4 ms 14548 KB Output is correct
35 Correct 3 ms 14424 KB Output is correct
36 Correct 3 ms 14424 KB Output is correct
37 Correct 3 ms 14428 KB Output is correct
38 Correct 4 ms 14428 KB Output is correct
39 Correct 4 ms 14472 KB Output is correct
40 Correct 5 ms 14428 KB Output is correct
41 Correct 4 ms 14548 KB Output is correct
42 Correct 3 ms 14428 KB Output is correct
43 Correct 3 ms 14544 KB Output is correct
44 Correct 3 ms 14428 KB Output is correct
45 Correct 3 ms 14428 KB Output is correct
46 Correct 4 ms 14428 KB Output is correct
47 Correct 4 ms 14428 KB Output is correct
48 Correct 4 ms 14428 KB Output is correct
49 Correct 3 ms 14428 KB Output is correct
50 Correct 3 ms 14428 KB Output is correct
51 Correct 3 ms 14428 KB Output is correct
52 Correct 3 ms 14428 KB Output is correct
53 Correct 4 ms 14428 KB Output is correct
54 Correct 3 ms 14428 KB Output is correct
55 Correct 3 ms 14428 KB Output is correct
56 Correct 3 ms 14428 KB Output is correct
57 Correct 3 ms 14428 KB Output is correct
58 Correct 3 ms 14428 KB Output is correct
59 Incorrect 3 ms 14428 KB 2nd lines differ - on the 82nd token, expected: '0', found: '1'
60 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14428 KB Output is correct
2 Correct 3 ms 14428 KB Output is correct
3 Correct 4 ms 14428 KB Output is correct
4 Correct 3 ms 14428 KB Output is correct
5 Correct 3 ms 14424 KB Output is correct
6 Correct 3 ms 14428 KB Output is correct
7 Correct 3 ms 14544 KB Output is correct
8 Correct 3 ms 14428 KB Output is correct
9 Correct 3 ms 14428 KB Output is correct
10 Correct 3 ms 14428 KB Output is correct
11 Correct 3 ms 14428 KB Output is correct
12 Correct 3 ms 14424 KB Output is correct
13 Correct 4 ms 14428 KB Output is correct
14 Correct 3 ms 14424 KB Output is correct
15 Correct 3 ms 14428 KB Output is correct
16 Correct 3 ms 14428 KB Output is correct
17 Correct 3 ms 14428 KB Output is correct
18 Correct 4 ms 14428 KB Output is correct
19 Correct 3 ms 14424 KB Output is correct
20 Correct 3 ms 14428 KB Output is correct
21 Correct 3 ms 14428 KB Output is correct
22 Correct 3 ms 14428 KB Output is correct
23 Correct 3 ms 14428 KB Output is correct
24 Correct 3 ms 14428 KB Output is correct
25 Correct 38 ms 14684 KB Output is correct
26 Correct 28 ms 14680 KB Output is correct
27 Correct 22 ms 14696 KB Output is correct
28 Correct 12 ms 14812 KB Output is correct
29 Correct 38 ms 14684 KB Output is correct
30 Correct 13 ms 14684 KB Output is correct
31 Correct 8 ms 14684 KB Output is correct
32 Incorrect 7 ms 14424 KB 2nd lines differ - on the 802nd token, expected: '0', found: '1'
33 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14424 KB Output is correct
2 Correct 3 ms 14428 KB Output is correct
3 Correct 3 ms 14428 KB Output is correct
4 Correct 3 ms 14428 KB Output is correct
5 Correct 3 ms 14428 KB Output is correct
6 Correct 3 ms 14428 KB Output is correct
7 Correct 3 ms 14428 KB Output is correct
8 Correct 3 ms 14428 KB Output is correct
9 Correct 4 ms 14544 KB Output is correct
10 Correct 4 ms 14428 KB Output is correct
11 Correct 3 ms 14428 KB Output is correct
12 Correct 3 ms 14424 KB Output is correct
13 Correct 3 ms 14428 KB Output is correct
14 Correct 3 ms 14544 KB Output is correct
15 Correct 3 ms 14428 KB Output is correct
16 Correct 3 ms 14428 KB Output is correct
17 Correct 3 ms 14428 KB Output is correct
18 Correct 3 ms 14428 KB Output is correct
19 Correct 3 ms 14424 KB Output is correct
20 Correct 4 ms 14428 KB Output is correct
21 Correct 3 ms 14424 KB Output is correct
22 Correct 3 ms 14428 KB Output is correct
23 Correct 3 ms 14428 KB Output is correct
24 Correct 3 ms 14428 KB Output is correct
25 Correct 4 ms 14428 KB Output is correct
26 Correct 3 ms 14424 KB Output is correct
27 Correct 3 ms 14428 KB Output is correct
28 Correct 3 ms 14428 KB Output is correct
29 Correct 3 ms 14428 KB Output is correct
30 Correct 3 ms 14428 KB Output is correct
31 Correct 3 ms 14428 KB Output is correct
32 Correct 3 ms 14428 KB Output is correct
33 Correct 3 ms 14624 KB Output is correct
34 Correct 4 ms 14548 KB Output is correct
35 Correct 3 ms 14424 KB Output is correct
36 Correct 3 ms 14424 KB Output is correct
37 Correct 3 ms 14428 KB Output is correct
38 Execution timed out 2059 ms 41312 KB Time limit exceeded
39 Halted 0 ms 0 KB -