답안 #945217

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
945217 2024-03-13T14:30:33 Z Nhoksocqt1 참나무 (IOI23_beechtree) C++17
14 / 100
2000 ms 43552 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], allEqual[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;
    }

    allEqual[u] = 1;
    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];
        allEqual[u] &= allEqual[v] & (sz(adj[v]) == 0 || col[v] == col[adj[v][0]]) & (col[v] == col[adj[u][0]]);
    }
}

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;
        }

        if(maxDepth[i] - depth[i] > 2) {
            ans.push_back(allEqual[i]);
            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*/ {
        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
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14684 KB Output is correct
2 Correct 3 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14684 KB Output is correct
2 Correct 4 ms 14684 KB Output is correct
3 Correct 3 ms 14680 KB Output is correct
4 Correct 3 ms 14680 KB Output is correct
5 Correct 3 ms 14684 KB Output is correct
6 Correct 3 ms 14684 KB Output is correct
7 Correct 3 ms 14684 KB Output is correct
8 Correct 3 ms 14680 KB Output is correct
9 Correct 3 ms 14684 KB Output is correct
10 Correct 5 ms 14680 KB Output is correct
11 Correct 4 ms 14684 KB Output is correct
12 Correct 4 ms 14684 KB Output is correct
13 Correct 3 ms 14680 KB Output is correct
14 Correct 4 ms 14680 KB Output is correct
15 Correct 3 ms 14684 KB Output is correct
16 Correct 3 ms 14684 KB Output is correct
17 Correct 4 ms 14684 KB Output is correct
18 Correct 4 ms 14684 KB Output is correct
19 Correct 4 ms 14684 KB Output is correct
20 Incorrect 4 ms 14684 KB 2nd lines differ - on the 1st token, expected: '1', found: '0'
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14684 KB Output is correct
2 Correct 4 ms 14684 KB Output is correct
3 Correct 3 ms 14680 KB Output is correct
4 Correct 3 ms 14680 KB Output is correct
5 Correct 3 ms 14684 KB Output is correct
6 Correct 3 ms 14684 KB Output is correct
7 Correct 70 ms 42832 KB Output is correct
8 Correct 74 ms 43552 KB Output is correct
9 Correct 4 ms 14684 KB Output is correct
10 Correct 3 ms 14684 KB Output is correct
11 Correct 4 ms 14680 KB Output is correct
12 Correct 3 ms 14684 KB Output is correct
13 Correct 4 ms 14940 KB Output is correct
14 Correct 5 ms 14940 KB Output is correct
15 Correct 4 ms 14936 KB Output is correct
16 Correct 4 ms 14936 KB Output is correct
17 Correct 79 ms 43236 KB Output is correct
18 Correct 68 ms 43448 KB Output is correct
19 Correct 70 ms 43468 KB Output is correct
20 Correct 89 ms 43464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14684 KB Output is correct
2 Correct 3 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 3 ms 14684 KB Output is correct
5 Correct 3 ms 14684 KB Output is correct
6 Correct 3 ms 14684 KB Output is correct
7 Correct 4 ms 14684 KB Output is correct
8 Correct 4 ms 14680 KB Output is correct
9 Correct 3 ms 14680 KB Output is correct
10 Correct 3 ms 14680 KB Output is correct
11 Correct 4 ms 14684 KB Output is correct
12 Correct 4 ms 14684 KB Output is correct
13 Correct 3 ms 14680 KB Output is correct
14 Correct 4 ms 14680 KB Output is correct
15 Correct 35 ms 18572 KB Output is correct
16 Correct 28 ms 18136 KB Output is correct
17 Correct 27 ms 18136 KB Output is correct
18 Correct 29 ms 18900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14684 KB Output is correct
2 Correct 4 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 3 ms 14680 KB Output is correct
5 Correct 70 ms 42832 KB Output is correct
6 Correct 74 ms 43552 KB Output is correct
7 Correct 3 ms 14684 KB Output is correct
8 Correct 3 ms 14684 KB Output is correct
9 Correct 5 ms 14684 KB Output is correct
10 Correct 4 ms 14684 KB Output is correct
11 Execution timed out 2062 ms 24292 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14684 KB Output is correct
2 Correct 3 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 3 ms 14684 KB Output is correct
5 Correct 4 ms 14684 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 3 ms 14680 KB Output is correct
8 Correct 3 ms 14684 KB Output is correct
9 Correct 3 ms 14684 KB Output is correct
10 Correct 3 ms 14684 KB Output is correct
11 Correct 3 ms 14680 KB Output is correct
12 Correct 3 ms 14684 KB Output is correct
13 Correct 5 ms 14680 KB Output is correct
14 Correct 4 ms 14684 KB Output is correct
15 Correct 4 ms 14684 KB Output is correct
16 Correct 3 ms 14680 KB Output is correct
17 Correct 4 ms 14680 KB Output is correct
18 Correct 3 ms 14684 KB Output is correct
19 Correct 3 ms 14684 KB Output is correct
20 Correct 4 ms 14684 KB Output is correct
21 Correct 4 ms 14684 KB Output is correct
22 Correct 4 ms 14684 KB Output is correct
23 Incorrect 4 ms 14684 KB 2nd lines differ - on the 1st token, expected: '1', found: '0'
24 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14684 KB Output is correct
2 Correct 4 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 3 ms 14680 KB Output is correct
5 Correct 3 ms 14684 KB Output is correct
6 Correct 5 ms 14680 KB Output is correct
7 Correct 4 ms 14684 KB Output is correct
8 Correct 4 ms 14684 KB Output is correct
9 Correct 3 ms 14680 KB Output is correct
10 Correct 4 ms 14680 KB Output is correct
11 Correct 3 ms 14684 KB Output is correct
12 Correct 3 ms 14684 KB Output is correct
13 Correct 4 ms 14684 KB Output is correct
14 Correct 4 ms 14684 KB Output is correct
15 Correct 4 ms 14684 KB Output is correct
16 Incorrect 4 ms 14684 KB 2nd lines differ - on the 1st token, expected: '1', found: '0'
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14684 KB Output is correct
2 Correct 3 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 3 ms 14684 KB Output is correct
5 Correct 4 ms 14684 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 3 ms 14680 KB Output is correct
8 Correct 3 ms 14684 KB Output is correct
9 Correct 3 ms 14684 KB Output is correct
10 Correct 3 ms 14684 KB Output is correct
11 Correct 3 ms 14680 KB Output is correct
12 Correct 3 ms 14684 KB Output is correct
13 Correct 5 ms 14680 KB Output is correct
14 Correct 4 ms 14684 KB Output is correct
15 Correct 4 ms 14684 KB Output is correct
16 Correct 3 ms 14680 KB Output is correct
17 Correct 4 ms 14680 KB Output is correct
18 Correct 3 ms 14684 KB Output is correct
19 Correct 3 ms 14684 KB Output is correct
20 Correct 4 ms 14684 KB Output is correct
21 Correct 4 ms 14684 KB Output is correct
22 Correct 4 ms 14684 KB Output is correct
23 Incorrect 4 ms 14684 KB 2nd lines differ - on the 1st token, expected: '1', found: '0'
24 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14684 KB Output is correct
2 Correct 4 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 3 ms 14680 KB Output is correct
5 Correct 3 ms 14684 KB Output is correct
6 Correct 5 ms 14680 KB Output is correct
7 Correct 4 ms 14684 KB Output is correct
8 Correct 4 ms 14684 KB Output is correct
9 Correct 3 ms 14680 KB Output is correct
10 Correct 4 ms 14680 KB Output is correct
11 Correct 3 ms 14684 KB Output is correct
12 Correct 3 ms 14684 KB Output is correct
13 Correct 4 ms 14684 KB Output is correct
14 Correct 4 ms 14684 KB Output is correct
15 Correct 4 ms 14684 KB Output is correct
16 Incorrect 4 ms 14684 KB 2nd lines differ - on the 1st token, expected: '1', found: '0'
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14684 KB Output is correct
2 Correct 3 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 3 ms 14684 KB Output is correct
5 Correct 4 ms 14684 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 3 ms 14680 KB Output is correct
8 Correct 3 ms 14684 KB Output is correct
9 Correct 3 ms 14684 KB Output is correct
10 Correct 3 ms 14684 KB Output is correct
11 Correct 3 ms 14680 KB Output is correct
12 Correct 3 ms 14684 KB Output is correct
13 Correct 5 ms 14680 KB Output is correct
14 Correct 4 ms 14684 KB Output is correct
15 Correct 4 ms 14684 KB Output is correct
16 Correct 3 ms 14680 KB Output is correct
17 Correct 4 ms 14680 KB Output is correct
18 Correct 3 ms 14684 KB Output is correct
19 Correct 3 ms 14684 KB Output is correct
20 Correct 4 ms 14684 KB Output is correct
21 Correct 4 ms 14684 KB Output is correct
22 Correct 4 ms 14684 KB Output is correct
23 Incorrect 4 ms 14684 KB 2nd lines differ - on the 1st token, expected: '1', found: '0'
24 Halted 0 ms 0 KB -