Submission #945205

# Submission time Handle Problem Language Result Execution time Memory
945205 2024-03-13T14:13:37 Z Nhoksocqt1 Beech Tree (IOI23_beechtree) C++17
9 / 100
74 ms 40812 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;
    }

    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:171:1: warning: control reaches end of non-void function [-Wreturn-type]
  171 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14424 KB Output is correct
2 Incorrect 4 ms 14428 KB 2nd lines differ - on the 1st token, expected: '0', found: '1'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14428 KB Output is correct
2 Correct 4 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 14528 KB Output is correct
6 Correct 4 ms 14424 KB Output is correct
7 Correct 3 ms 14548 KB Output is correct
8 Correct 3 ms 14468 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 4 ms 14424 KB Output is correct
15 Correct 3 ms 14424 KB Output is correct
16 Correct 3 ms 14428 KB Output is correct
17 Correct 4 ms 14424 KB Output is correct
18 Correct 3 ms 14428 KB Output is correct
19 Correct 3 ms 14428 KB Output is correct
20 Correct 3 ms 14424 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 14428 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 4 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 4 ms 14428 KB Output is correct
33 Correct 4 ms 14428 KB Output is correct
34 Correct 3 ms 14428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14428 KB Output is correct
2 Correct 4 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 14528 KB Output is correct
6 Correct 4 ms 14424 KB Output is correct
7 Incorrect 74 ms 40812 KB 2nd lines differ - on the 1st token, expected: '0', found: '1'
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14528 KB Output is correct
2 Correct 3 ms 14428 KB Output is correct
3 Incorrect 3 ms 14428 KB 2nd lines differ - on the 1st token, expected: '0', found: '1'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14428 KB Output is correct
2 Correct 4 ms 14428 KB Output is correct
3 Correct 3 ms 14548 KB Output is correct
4 Correct 3 ms 14468 KB Output is correct
5 Incorrect 74 ms 40812 KB 2nd lines differ - on the 1st token, expected: '0', found: '1'
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14424 KB Output is correct
2 Incorrect 4 ms 14428 KB 2nd lines differ - on the 1st token, expected: '0', found: '1'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14428 KB Output is correct
2 Correct 4 ms 14428 KB Output is correct
3 Correct 3 ms 14548 KB Output is correct
4 Correct 3 ms 14468 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 14424 KB Output is correct
9 Correct 4 ms 14428 KB Output is correct
10 Correct 4 ms 14424 KB Output is correct
11 Correct 3 ms 14424 KB Output is correct
12 Correct 3 ms 14428 KB Output is correct
13 Correct 4 ms 14424 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 3 ms 14428 KB Output is correct
18 Correct 4 ms 14428 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 3 ms 14428 KB Output is correct
23 Correct 3 ms 14428 KB Output is correct
24 Correct 4 ms 14428 KB Output is correct
25 Correct 10 ms 14684 KB Output is correct
26 Incorrect 5 ms 14684 KB 2nd lines differ - on the 1st token, expected: '0', found: '1'
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14424 KB Output is correct
2 Incorrect 4 ms 14428 KB 2nd lines differ - on the 1st token, expected: '0', found: '1'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14428 KB Output is correct
2 Correct 4 ms 14428 KB Output is correct
3 Correct 3 ms 14548 KB Output is correct
4 Correct 3 ms 14468 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 14424 KB Output is correct
9 Correct 4 ms 14428 KB Output is correct
10 Correct 4 ms 14424 KB Output is correct
11 Correct 3 ms 14424 KB Output is correct
12 Correct 3 ms 14428 KB Output is correct
13 Correct 4 ms 14424 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 3 ms 14428 KB Output is correct
18 Correct 4 ms 14428 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 3 ms 14428 KB Output is correct
23 Correct 3 ms 14428 KB Output is correct
24 Correct 4 ms 14428 KB Output is correct
25 Correct 10 ms 14684 KB Output is correct
26 Incorrect 5 ms 14684 KB 2nd lines differ - on the 1st token, expected: '0', found: '1'
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14424 KB Output is correct
2 Incorrect 4 ms 14428 KB 2nd lines differ - on the 1st token, expected: '0', found: '1'
3 Halted 0 ms 0 KB -