Submission #1115821

#TimeUsernameProblemLanguageResultExecution timeMemory
1115821vjudge1Cat in a tree (BOI17_catinatree)C++17
100 / 100
94 ms22188 KiB
#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> g[200005]; int d; pair<int, int> f[200005]; void dfs(int x) { if (g[x].size() == 0) { f[x] = {1, 0}; return; } int c = 0; vector<int> v; for (int w : g[x]) { dfs(w); c += f[w].first; v.push_back(f[w].second + 1); } sort(v.begin(), v.end()); int j = -1; for (int i = 0; i < v.size() - 1; i++) { if (v[i] + v[i + 1] < d) { c--; } else { j = i; break; } } if (j == -1) j = v.size() - 1; if (v[j] >= d) { v[j] = 0; c++; } f[x] = {c, v[j]}; /*cout << x << " " << f[x].first << " " << j << " " << v[j] << '\n'; for (int w : v) cout << w << " "; cout << '\n';*/ } int main() { int n; cin >> n >> d; for (int i = 1; i < n; i++) { int x; cin >> x; g[x].push_back(i); } dfs(0); cout << f[0].first; }

Compilation message (stderr)

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