Submission #1039043

#TimeUsernameProblemLanguageResultExecution timeMemory
1039043HanksburgerCat in a tree (BOI17_catinatree)C++17
100 / 100
36 ms21844 KiB
#include <bits/stdc++.h>
using namespace std;
vector<int> j[200005];
int n, d, s, x;
int f(int u)
{
    s++;
    vector<int> a;
    a.push_back(0);
    for (int v:j[u])
        a.push_back(f(v)+1);
    sort(a.begin(), a.end());
    int m=0;
    while (m+1<a.size() && a[m]+a[m+1]<d)
        m++, s--;
    return a[m];
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> d;
    for (int i=1; i<n; i++)
    {
        cin >> x;
        j[x].push_back(i);
    }
    f(0);
    cout << s;
}

Compilation message (stderr)

catinatree.cpp: In function 'int f(int)':
catinatree.cpp:14:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 |     while (m+1<a.size() && a[m]+a[m+1]<d)
      |            ~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...