Submission #643579

# Submission time Handle Problem Language Result Execution time Memory
643579 2022-09-22T14:26:02 Z Vu_CG_Coder Cat in a tree (BOI17_catinatree) C++14
51 / 100
602 ms 33596 KB
/* [Author : Hoang Duy Vu] */
#include <bits/stdc++.h>
#define All(x) (x).begin(),(x).end()
#define ll long long
#define C make_pair
#define fi first
#define se second
#define two second.first
#define thr second.second
#define TASK "txt"
using namespace std;
template<typename T> bool maximize(T &res, const T &val) {
    if (res < val) { res = val; return true; } return false; }
template<typename T> bool minimize(T &res, const T &val) {
    if (res > val) { res = val; return true; } return false; }

typedef pair<int,int> ii;
typedef pair<int,ii> iii;
const int LOG = 20;
const int INF = 1e9 + 7;
const ll LNF = 1e18 + 7;
const int mod = 1e9 + 7;
const int N = 200100;

vector <int> a[N];
int dp[1600][3][1600] = {0};
int t[N] = {0};
int high[N] = {0};
int n , k;

void input()
{
    cin >> n >> k;
    for (int i = 1 ; i < n ; i++)
    {
        int x;
        cin >> x;
        a[x].push_back(i);
        a[i].push_back(x);
    }
}

void dfs(int u ,int par)
{
    memset(dp[u],0,sizeof(dp[u]));
    int cnt = 0;

    for (int v : a[u])
    if (v != par)
    {
        dfs(v,u);
        int New = (cnt + 1)&1;
        memset(dp[u][New],0,sizeof(dp[New][0]));
        maximize(high[u],high[v] + 1);

        for (int i = 0 ; i <= high[u] ; i++)
        {
            dp[u][New][i] = dp[u][cnt][i];

            for (int j = max(i - 1,0) ; j <= high[v] ; j++)
            {
                int x = j + 1;

           //     if (u == 7) cout << u << " " << high[v] << " " << v << " " << i << " " << x << " " << k - x << " " << dp[v][t[v]][j] <<"\n";
                maximize(dp[u][New][i],dp[u][cnt][max(i,k - x)] + dp[v][t[v]][j]);
            }


        }
      //  if (u == 7) cout << v << " " << dp[u][New][1] << "\n";

        cnt = New;
    }

    t[u] = cnt;

    maximize(dp[u][t[u]][0],dp[u][t[u]][k] + 1);

    // cout << u << ":";

    // for (int i = 0 ; i <= high[u] ; i++) cout << dp[u][t[u]][i] << " ";
    // cout << "\n\n";
}

void solve()
{
    dfs(0,0);

    int res = 0;
    for (int i = 0 ; i <= high[0] ; i++) maximize(res,dp[0][t[0]][i]);

    cout << res;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL); cout.tie(NULL);
    if(fopen(TASK".inp", "r")){
    freopen(TASK".inp","r",stdin);
    freopen(TASK".out","w",stdout);
    }

    int tk;
    tk = 1;
   //cin >> tk;
    while (tk--)
    {
        input();
        solve();
    }
    return 0;
}

Compilation message

catinatree.cpp: In function 'int main()':
catinatree.cpp:100:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |     freopen(TASK".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:101:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |     freopen(TASK".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5152 KB Output is correct
2 Correct 4 ms 5204 KB Output is correct
3 Correct 3 ms 5148 KB Output is correct
4 Correct 4 ms 5144 KB Output is correct
5 Correct 3 ms 5224 KB Output is correct
6 Correct 2 ms 5204 KB Output is correct
7 Correct 3 ms 5332 KB Output is correct
8 Correct 3 ms 5264 KB Output is correct
9 Correct 3 ms 5276 KB Output is correct
10 Correct 3 ms 5332 KB Output is correct
11 Correct 3 ms 5332 KB Output is correct
12 Correct 4 ms 5332 KB Output is correct
13 Correct 3 ms 5280 KB Output is correct
14 Correct 3 ms 5280 KB Output is correct
15 Correct 3 ms 5332 KB Output is correct
16 Correct 3 ms 5332 KB Output is correct
17 Correct 3 ms 5332 KB Output is correct
18 Correct 4 ms 5332 KB Output is correct
19 Correct 3 ms 5332 KB Output is correct
20 Correct 3 ms 5332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5152 KB Output is correct
2 Correct 4 ms 5204 KB Output is correct
3 Correct 3 ms 5148 KB Output is correct
4 Correct 4 ms 5144 KB Output is correct
5 Correct 3 ms 5224 KB Output is correct
6 Correct 2 ms 5204 KB Output is correct
7 Correct 3 ms 5332 KB Output is correct
8 Correct 3 ms 5264 KB Output is correct
9 Correct 3 ms 5276 KB Output is correct
10 Correct 3 ms 5332 KB Output is correct
11 Correct 3 ms 5332 KB Output is correct
12 Correct 4 ms 5332 KB Output is correct
13 Correct 3 ms 5280 KB Output is correct
14 Correct 3 ms 5280 KB Output is correct
15 Correct 3 ms 5332 KB Output is correct
16 Correct 3 ms 5332 KB Output is correct
17 Correct 3 ms 5332 KB Output is correct
18 Correct 4 ms 5332 KB Output is correct
19 Correct 3 ms 5332 KB Output is correct
20 Correct 3 ms 5332 KB Output is correct
21 Correct 602 ms 33468 KB Output is correct
22 Correct 8 ms 14368 KB Output is correct
23 Correct 8 ms 14420 KB Output is correct
24 Correct 7 ms 14368 KB Output is correct
25 Correct 11 ms 23840 KB Output is correct
26 Correct 11 ms 23788 KB Output is correct
27 Correct 10 ms 23840 KB Output is correct
28 Correct 15 ms 33236 KB Output is correct
29 Correct 15 ms 33180 KB Output is correct
30 Correct 14 ms 33236 KB Output is correct
31 Correct 15 ms 33236 KB Output is correct
32 Correct 15 ms 32704 KB Output is correct
33 Correct 14 ms 32596 KB Output is correct
34 Correct 13 ms 31336 KB Output is correct
35 Correct 14 ms 31316 KB Output is correct
36 Correct 15 ms 33236 KB Output is correct
37 Correct 20 ms 33300 KB Output is correct
38 Correct 21 ms 33228 KB Output is correct
39 Correct 39 ms 33292 KB Output is correct
40 Correct 592 ms 33596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5152 KB Output is correct
2 Correct 4 ms 5204 KB Output is correct
3 Correct 3 ms 5148 KB Output is correct
4 Correct 4 ms 5144 KB Output is correct
5 Correct 3 ms 5224 KB Output is correct
6 Correct 2 ms 5204 KB Output is correct
7 Correct 3 ms 5332 KB Output is correct
8 Correct 3 ms 5264 KB Output is correct
9 Correct 3 ms 5276 KB Output is correct
10 Correct 3 ms 5332 KB Output is correct
11 Correct 3 ms 5332 KB Output is correct
12 Correct 4 ms 5332 KB Output is correct
13 Correct 3 ms 5280 KB Output is correct
14 Correct 3 ms 5280 KB Output is correct
15 Correct 3 ms 5332 KB Output is correct
16 Correct 3 ms 5332 KB Output is correct
17 Correct 3 ms 5332 KB Output is correct
18 Correct 4 ms 5332 KB Output is correct
19 Correct 3 ms 5332 KB Output is correct
20 Correct 3 ms 5332 KB Output is correct
21 Correct 602 ms 33468 KB Output is correct
22 Correct 8 ms 14368 KB Output is correct
23 Correct 8 ms 14420 KB Output is correct
24 Correct 7 ms 14368 KB Output is correct
25 Correct 11 ms 23840 KB Output is correct
26 Correct 11 ms 23788 KB Output is correct
27 Correct 10 ms 23840 KB Output is correct
28 Correct 15 ms 33236 KB Output is correct
29 Correct 15 ms 33180 KB Output is correct
30 Correct 14 ms 33236 KB Output is correct
31 Correct 15 ms 33236 KB Output is correct
32 Correct 15 ms 32704 KB Output is correct
33 Correct 14 ms 32596 KB Output is correct
34 Correct 13 ms 31336 KB Output is correct
35 Correct 14 ms 31316 KB Output is correct
36 Correct 15 ms 33236 KB Output is correct
37 Correct 20 ms 33300 KB Output is correct
38 Correct 21 ms 33228 KB Output is correct
39 Correct 39 ms 33292 KB Output is correct
40 Correct 592 ms 33596 KB Output is correct
41 Runtime error 48 ms 23712 KB Execution killed with signal 11
42 Halted 0 ms 0 KB -