Submission #1296543

#TimeUsernameProblemLanguageResultExecution timeMemory
1296543hiepsimauhongCat in a tree (BOI17_catinatree)C++20
51 / 100
1108 ms342376 KiB
#include <bits/stdc++.h>

using namespace std;

//#define int long long

#define FOR(I, L, R) for(int I(L) ; I <= (int)R ; ++I)
#define FOD(I, R, L) for(int I(R) ; I >= (int)L ; --I)
#define FOA(I, A) for(auto &I : A)

#define print(A,L,R) FOR(OK, L, R){if(A[OK]<=-oo / 10||A[OK]>=oo)cout<<"- ";else cout<<A[OK]<<' ';}cout<<'\n';
#define prints(A) FOA(OK, A){cout<<OK<<' ';}cout << '\n';
#define printz(A,L,R) FOR(OK, 0, L){FOR(KO, 0, R){if(A[OK][KO]>-oo&&A[OK][KO]<oo)cout<<A[OK][KO]<<' ';else cout << "- ";} cout << '\n';}cout << '\n';

#define fs first
#define sd second
#define ii pair<int,int>
#define iii pair<int, ii>
#define all(A) A.begin(), A.end()
#define quickly ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FILE "catinatree"
#define pow ladsf

const int N = 2e5 + 5;
const int SQRT = 448;

const int mod = 1e9 + 7;
const int oo = 1e9;

int n, k, limit, ans;
int dp[N][SQRT], sz[N], f[SQRT];
vector<int> g[N];

void DFS(int u){
        sz[u] = 1;

        FOA(v, g[u]){
                DFS(v);
        }
        dp[u][0] = 1;

        FOA(v, g[u]){
                FOR(i, 0, min(limit, sz[u])){
                        f[i] = dp[u][i];
                }

                FOR(i, 0, min(limit, sz[u])){
                        FOR(j, 0, min(limit, sz[v])){
                                if(i + j + 1 >= k){
                                        dp[u][min(i, j + 1)] = max(dp[u][min(i, j + 1)], f[i] + dp[v][j]);
                                }
                        }
                }

                FOR(i, 0, min(limit, sz[v])){
                        dp[u][i + 1] = max(dp[u][i + 1], dp[v][i]);
                }

                sz[u] += sz[v];
        }

        FOR(i, 0, min(limit, sz[u])) ans = max(ans, dp[u][i]);
}

void DFS2(int u){
        sz[u] = 1;

        FOR(i, 0, limit) dp[u][i] = -oo;
        dp[u][0] = oo;
        dp[u][1] = 0;

        FOA(v, g[u]){
                DFS2(v);

                FOD(i, min(sz[u], limit), 0){
                        if(dp[u][i] < 0) continue;

                        FOR(j, 0, min(sz[v], limit)){
                                if(dp[u][i] + dp[v][j] + 1 >= k){
                                        dp[u][i + j] = max(dp[u][i + j], min(dp[u][i], dp[v][j] + 1));
                                }
                        }
                }
                sz[u] += sz[v];
        }

        FOR(i, 0, min(limit, sz[u])){
                if(dp[u][i] >= 0){
                        ans = max(ans, i);
                }
        }
}

signed main(){ quickly
        if(fopen(FILE".inp", "r")){
                freopen(FILE".inp", "r", stdin);
                freopen(FILE".out", "w", stdout);
        }

        cin >> n >> k;
        limit = (n - 1) / k + 1;
        ++limit;

        FOR(i, 1, n - 1){
                int x; cin >> x;
                g[x].push_back(i);
        }

        if(limit <= k){
                DFS2(0);
        }
        else{
                DFS(0);
        }

        cout << ans;
}

Compilation message (stderr)

catinatree.cpp: In function 'int main()':
catinatree.cpp:96:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |                 freopen(FILE".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:97:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |                 freopen(FILE".out", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...