Submission #703143

#TimeUsernameProblemLanguageResultExecution timeMemory
703143browntoadCat in a tree (BOI17_catinatree)C++14
100 / 100
151 ms35512 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5+5; int n, d; vector<int> graph[maxn], dp(maxn); multiset<int> pen[maxn]; // pending void dfs(int x, int dep){ for (int i=0; i<graph[x].size(); i++){ dfs(graph[x][i], dep+1); dp[x]+=dp[graph[x][i]]; if (pen[graph[x][i]].size() > pen[x].size()) swap(pen[x], pen[graph[x][i]]); for (int j:pen[graph[x][i]]) pen[x].insert(j); } while(pen[x].size()>=1 && (*prev(pen[x].end()))>=dep+d) { dp[x]++; pen[x].erase(prev(pen[x].end())); } while(pen[x].size()>=2){ if ((*pen[x].begin())-dep+(*next(pen[x].begin()))-dep < d){ pen[x].erase(pen[x].begin()); } else break; } if (pen[x].size()==0) pen[x].insert(dep); } int main(){ cin>>n>>d; for (int i=2; i<=n; i++){ int x; cin>>x; graph[x+1].push_back(i); } dfs(1, 0); cout<<dp[1]+pen[1].size()<<endl; }

Compilation message (stderr)

catinatree.cpp: In function 'void dfs(int, int)':
catinatree.cpp:8:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 |     for (int i=0; i<graph[x].size(); i++){
      |                   ~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...