Submission #645267

# Submission time Handle Problem Language Result Execution time Memory
645267 2022-09-26T15:22:06 Z Vu_CG_Coder Cat in a tree (BOI17_catinatree) C++14
51 / 100
58 ms 22876 KB
/* [Author : Hoang Duy Vu] - THPT Chuyen Nguyen Du    */
/* [2022 - 12TH GO VOI 23] - From Nguyen Du with love */
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cmath>
#include <cstdio> 
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <iomanip>
#include <numeric>
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; i++)
#define FORE(i,b, a) for(int i = (b), _a = (a); i >= _a; i--)
#define All(x) (x).begin(),(x).end()
#define __ZuyVuNee int main()
#define MASK(i) (1ll<<(i))
#define ll long long
#define se second
#define C make_pair
#define fi first
#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 = 17;
const int INF = 1e9 + 7;
const ll LNF = 1e18 + 7;
const int mod = 1e9 + 7;
const int N = 100100;

int n , k;
int par[N][LOG + 2];
int high[N];
int Pa[N] = {0};
int cnt[N] = {0};
int f[N] = {0};
int Mn[N] = {0};
ii H[N];

vector <int> a[N];

void findhigh(int u)
{
    for (int v : a[u])
    if (v != par[u][0])
    {
        high[v] = high[u] + 1;
        H[v] = C(high[v],v);
        par[v][0] = u;
        findhigh(v);
    }
}

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

int get(int u , int v)
{
    if (high[u] < high[v]) return get(v,u);

    for (int i = LOG ; i >= 0 ; i--)
    if (high[par[u][i]] >= high[v]) u = par[u][i];

    if (u == v) return u;

    for (int i = LOG ; i >= 0 ; i--)
    if (par[u][i] != par[v][i])
    {
        u = par[u][i];
        v = par[v][i];
    }

    return par[u][0];
}

void subtree(int u , int P)
{
    cnt[u] = 1;

    for (int v : a[u])
    if (v != P && !f[v]) 
    {
        subtree(v,u);
        cnt[u] += cnt[v];
    }
}

int FindCentroid(int u , int P , int t)
{
    for (int v : a[u])
    if (!f[v] && v != P) 
        if (cnt[v]*2 > t) return FindCentroid(v,u,t);
    return u;
}

void Centroid(int u , int P)
{
    subtree(u,u);

    int root = FindCentroid(u,u,cnt[u]);

    Pa[root] = P;

    f[root] = 1;

    for (int v : a[root])
    if (!f[v]) Centroid(v,root);
}


void update(int u)
{
    int x = u;
    while (x)
    {
        minimize(Mn[x],high[u] + high[x] - 2*high[get(u,x)]);
        x = Pa[x];
    }
}

int getMin(int u)
{
    int x = u;
    int res = INF;
    while (x)
    {
        minimize(res,high[u] + high[x] - 2*high[get(u,x)] + Mn[x]);
        x = Pa[x];
    }

    return res;
}

void solve()
{
    high[0] = -1;
    high[1] = 0;
    H[1] = C(0,1);
    par[1][0] = 0;
    findhigh(1);

    for (int j = 1 ; j <= LOG ; j++)
        for (int i = 1 ; i <= n ; i++)
            par[i][j] = par[par[i][j - 1]][j - 1];
    
    Centroid(1,0);

    memset(Mn,0x3f,sizeof(Mn));

    sort(H + 1 , H + n + 1,greater<ii>());

    int res = 1;
    update(H[1].se);

    for (int i = 2 ; i <= n ; i++)
    {
        int x = H[i].fi;
        int y = H[i].se;
       if (getMin(y) >= k) 
       {
            update(y);
            res++;
       }
    }

    cout << res;
}

__ZuyVuNee
{
    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)
    {
        tk--;
        write();
        solve();
    }
    return 0;
}




Compilation message

catinatree.cpp: In function 'void solve()':
catinatree.cpp:182:13: warning: unused variable 'x' [-Wunused-variable]
  182 |         int x = H[i].fi;
      |             ^
catinatree.cpp: In function 'int main()':
catinatree.cpp:199:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  199 |     freopen(TASK".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:200:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  200 |     freopen(TASK".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 2 ms 3028 KB Output is correct
4 Correct 2 ms 3028 KB Output is correct
5 Correct 2 ms 3028 KB Output is correct
6 Correct 2 ms 3064 KB Output is correct
7 Correct 2 ms 3028 KB Output is correct
8 Correct 3 ms 3064 KB Output is correct
9 Correct 2 ms 3060 KB Output is correct
10 Correct 2 ms 3028 KB Output is correct
11 Correct 2 ms 3028 KB Output is correct
12 Correct 2 ms 3064 KB Output is correct
13 Correct 3 ms 3028 KB Output is correct
14 Correct 2 ms 3028 KB Output is correct
15 Correct 3 ms 3028 KB Output is correct
16 Correct 2 ms 3064 KB Output is correct
17 Correct 2 ms 3028 KB Output is correct
18 Correct 2 ms 3064 KB Output is correct
19 Correct 2 ms 3064 KB Output is correct
20 Correct 2 ms 3064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 2 ms 3028 KB Output is correct
4 Correct 2 ms 3028 KB Output is correct
5 Correct 2 ms 3028 KB Output is correct
6 Correct 2 ms 3064 KB Output is correct
7 Correct 2 ms 3028 KB Output is correct
8 Correct 3 ms 3064 KB Output is correct
9 Correct 2 ms 3060 KB Output is correct
10 Correct 2 ms 3028 KB Output is correct
11 Correct 2 ms 3028 KB Output is correct
12 Correct 2 ms 3064 KB Output is correct
13 Correct 3 ms 3028 KB Output is correct
14 Correct 2 ms 3028 KB Output is correct
15 Correct 3 ms 3028 KB Output is correct
16 Correct 2 ms 3064 KB Output is correct
17 Correct 2 ms 3028 KB Output is correct
18 Correct 2 ms 3064 KB Output is correct
19 Correct 2 ms 3064 KB Output is correct
20 Correct 2 ms 3064 KB Output is correct
21 Correct 4 ms 3412 KB Output is correct
22 Correct 2 ms 3156 KB Output is correct
23 Correct 3 ms 3156 KB Output is correct
24 Correct 2 ms 3156 KB Output is correct
25 Correct 4 ms 3156 KB Output is correct
26 Correct 3 ms 3196 KB Output is correct
27 Correct 3 ms 3156 KB Output is correct
28 Correct 3 ms 3284 KB Output is correct
29 Correct 4 ms 3200 KB Output is correct
30 Correct 3 ms 3284 KB Output is correct
31 Correct 3 ms 3196 KB Output is correct
32 Correct 3 ms 3188 KB Output is correct
33 Correct 3 ms 3284 KB Output is correct
34 Correct 3 ms 3284 KB Output is correct
35 Correct 3 ms 3188 KB Output is correct
36 Correct 3 ms 3284 KB Output is correct
37 Correct 4 ms 3284 KB Output is correct
38 Correct 5 ms 3284 KB Output is correct
39 Correct 5 ms 3324 KB Output is correct
40 Correct 5 ms 3324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 2 ms 3028 KB Output is correct
4 Correct 2 ms 3028 KB Output is correct
5 Correct 2 ms 3028 KB Output is correct
6 Correct 2 ms 3064 KB Output is correct
7 Correct 2 ms 3028 KB Output is correct
8 Correct 3 ms 3064 KB Output is correct
9 Correct 2 ms 3060 KB Output is correct
10 Correct 2 ms 3028 KB Output is correct
11 Correct 2 ms 3028 KB Output is correct
12 Correct 2 ms 3064 KB Output is correct
13 Correct 3 ms 3028 KB Output is correct
14 Correct 2 ms 3028 KB Output is correct
15 Correct 3 ms 3028 KB Output is correct
16 Correct 2 ms 3064 KB Output is correct
17 Correct 2 ms 3028 KB Output is correct
18 Correct 2 ms 3064 KB Output is correct
19 Correct 2 ms 3064 KB Output is correct
20 Correct 2 ms 3064 KB Output is correct
21 Correct 4 ms 3412 KB Output is correct
22 Correct 2 ms 3156 KB Output is correct
23 Correct 3 ms 3156 KB Output is correct
24 Correct 2 ms 3156 KB Output is correct
25 Correct 4 ms 3156 KB Output is correct
26 Correct 3 ms 3196 KB Output is correct
27 Correct 3 ms 3156 KB Output is correct
28 Correct 3 ms 3284 KB Output is correct
29 Correct 4 ms 3200 KB Output is correct
30 Correct 3 ms 3284 KB Output is correct
31 Correct 3 ms 3196 KB Output is correct
32 Correct 3 ms 3188 KB Output is correct
33 Correct 3 ms 3284 KB Output is correct
34 Correct 3 ms 3284 KB Output is correct
35 Correct 3 ms 3188 KB Output is correct
36 Correct 3 ms 3284 KB Output is correct
37 Correct 4 ms 3284 KB Output is correct
38 Correct 5 ms 3284 KB Output is correct
39 Correct 5 ms 3324 KB Output is correct
40 Correct 5 ms 3324 KB Output is correct
41 Runtime error 58 ms 22876 KB Execution killed with signal 11
42 Halted 0 ms 0 KB -