Submission #1296484

#TimeUsernameProblemLanguageResultExecution timeMemory
1296484hiepsimauhongCat in a tree (BOI17_catinatree)C++20
11 / 100
6 ms4672 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 = 2000 + 5;
const int SQRT = 320;

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

struct Modint{
        int x;

        Modint(){x = 0;}
        Modint(int _x){ x = (_x % mod + mod) % mod; }

        Modint operator + (const Modint &other) const{ return (x + other.x) % mod; }
        Modint operator - (const Modint &other) const{ return (x - other.x + mod) % mod; }
        Modint operator * (const Modint &other) const{ return (1LL * x * other.x) % mod; }

        void operator += (const Modint &other){ *this = *this + other; }
        void operator -= (const Modint &other){ *this = *this - other; }
        void operator *= (const Modint &other){ *this = *this * other; }

        friend ostream& operator << (ostream& os, const Modint &t){ return os << t.x; }
};

int n, k;
vector<int> g[N];
int dp[N][SQRT + 5];

namespace LessSQRT{
        static int pre[N][SQRT + 5], h[N];
        static int f[SQRT + 5];

        void DFS(int u, int par){
                FOA(v, g[u]){
                        if(v == par){
                                continue;
                        }
                        DFS(v, u);
                        h[u] = max(h[u], h[v] + 1);
                }

                FOR(i, 0, min(k + 1, h[u])){
                        f[i] = 0;
                        FOA(v, g[u]){
                                if(v == par){
                                        continue;
                                }
                                f[i] += pre[v][max({0LL, i - 1, k - i})];
                        }
                }
                dp[u][0] = f[0] + 1;

                FOA(v, g[u]){
                        if(v == par){
                                continue;
                        }
                        FOR(i, 1, min(k, h[u])){
                                dp[u][i] = max(dp[u][i], f[i] + dp[v][i - 1] - pre[v][max({0LL, i - 1, k - i})]);
                        }
                }
                if(h[u] > k) dp[u][k + 1] = f[k + 1];

                FOD(i, min(k + 1, h[u]), 0){
                        pre[u][i] = max(dp[u][i], pre[u][i + 1]);
                }
        }

        void solve(){
                DFS(0, -1);

                cout << pre[0][0];
        }
}

namespace MoreSQRT{
        static int sz[N];

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

                dp[u][0] = oo;
                dp[u][1] = 0;

                FOR(i, 2, n){
                        dp[u][i] = -oo;
                }

                FOA(v, g[u]){
                        if(v == par){
                                continue;
                        }
                        DFS(v, u);
                }

                FOA(v, g[u]){
                        if(v == par){
                                continue;
                        }

                        FOD(i, sz[u], 0){
                                FOR(j, 0, sz[v]){
                                        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];
                }
        }

        void solve(){
                DFS(0, -1);

                FOD(i, n, 0){
                        if(dp[0][i] >= 0){
                                cout << i;
                                return;
                        }
                }
        }
}

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

        cin >> n >> k;
        --k;

        FOR(v, 1, n - 1){
                int u;
                cin >> u;

                g[u].push_back(v);
                g[v].push_back(u);
        }

        if(k < SQRT){
                LessSQRT::solve();
        }
        else{
                MoreSQRT::solve();
        }
}

Compilation message (stderr)

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