Submission #130384

#TimeUsernameProblemLanguageResultExecution timeMemory
130384roseanne_pcyCat in a tree (BOI17_catinatree)C++14
100 / 100
317 ms51676 KiB
//Power Of Ninja Go
#include <bits/stdc++.h>
//#ifdef atom #else #endif
using namespace std;
typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector< ii > vii;
#define X first
#define Y second
#define pb push_back
int n, k;
const int maxn = 2e5+5;
vi adj[maxn];
int h[maxn];
multiset<int> bst[maxn];
void dfs(int u)
{
    for(auto v : adj[u])
    {
        h[v] = 1+h[u];
        dfs(v);
    }
}
void solve(int u)
{
    for(auto v : adj[u]) solve(v);
    int big = -1;
    for(auto v : adj[u])
    {
        if(big == -1 || bst[v].size()> bst[big].size()) big = v;
    }
    if(big == -1) return;
    swap(bst[u], bst[big]);
    for(auto v : adj[u])
    {
        while(!bst[v].empty() && !bst[u].empty() && *bst[u].begin() + *bst[v].begin() - 2*h[u]< k)
        {
            if(*bst[u].begin()< *bst[v].begin()) bst[u].erase(bst[u].begin());
            else bst[v].erase(bst[v].begin());
        }
        for(auto x : bst[v]) bst[u].insert(x);
    }
}
int main()
{
    //#ifndef atom freopen(".in", "r", stdin); freopen(".out", "w", stdout); #endif
    scanf("%d %d", &n, &k);
    for(int i = 2; i<= n; i++)
    {
        int x; scanf("%d", &x);
        x++;
        adj[x].pb(i);
    }
    dfs(1);
    for(int i = 1; i<= n; i++) bst[i].insert(h[i]);
    solve(1); printf("%d\n", bst[1].size());
}

Compilation message (stderr)

catinatree.cpp: In function 'int main()':
catinatree.cpp:54:43: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::multiset<int>::size_type {aka long unsigned int}' [-Wformat=]
     solve(1); printf("%d\n", bst[1].size());
                              ~~~~~~~~~~~~~^
catinatree.cpp:45:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &k);
     ~~~~~^~~~~~~~~~~~~~~~~
catinatree.cpp:48:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         int x; scanf("%d", &x);
                ~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...