Submission #1106335

#TimeUsernameProblemLanguageResultExecution timeMemory
1106335CodeLakVNCat in a tree (BOI17_catinatree)C++17
51 / 100
1078 ms289732 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define name "main" #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FOD(i, b, a) for (int i = (b); i >= (a); i--) template <class U, class V> bool maximize(U &x, V y) { if (x < y) { x = y; return true; } return false; } template <class U, class V> bool minimize(U &x, V y) { if (x > y) { x = y; return true; } return false; } const int INF = 1e9; const int N = 2e5 + 5; int numNode, limit; vector<int> adj[N]; int child[N], centroidParent[N]; bool del[N]; int countChild(int u, int p) { child[u] = 1; for (int v : adj[u]) { if (v == p || del[v]) continue; child[u] += countChild(v, u); } return child[u]; } int findCentroid(int u, int p, int m) { for (int v : adj[u]) { if(v == p || del[v]) continue; if (child[v] > m / 2) return findCentroid(v, u, m); } return u; } map<int, int> dist[N]; void calcDist(int u, int p, int root) { for (int v : adj[u]) if (v != p) { dist[root][v] = dist[root][u] + 1; calcDist(v, u, root); } } int centroidDecomp(int u) { int m = countChild(u, -1); u = findCentroid(u, -1, m); del[u] = 1; calcDist(u, -1, u); for (int v : adj[u]) { if (del[v]) continue; int x = centroidDecomp(v); centroidParent[x] = u; } return u; } int minDistFromCentroid[N]; int getDistToSet(int u) { int curMinDist = INF; int centroid = u; while (centroid != 0) { minimize(curMinDist, dist[centroid][u] + minDistFromCentroid[centroid]); centroid = centroidParent[centroid]; } return curMinDist; } void addToSet(int u) { int centroid = u; while (centroid != 0) { minimize(minDistFromCentroid[centroid], dist[centroid][u]); centroid = centroidParent[centroid]; } } int depth[N]; void dfsDepth(int u, int p) { for (int v : adj[u]) { if (v == p) continue; depth[v] = depth[u] + 1; dfsDepth(v, u); } } bool cmp(int &x, int &y) { return depth[x] > depth[y]; } int main() { ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0); if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } cin >> numNode >> limit; FOR(i, 2, numNode) { int u; cin >> u; u++; adj[u].push_back(i); adj[i].push_back(u); } int root = centroidDecomp(1); centroidParent[root] = 0; vector<int> nodes; FOR(u, 1, numNode) nodes.push_back(u); dfsDepth(1, -1); sort(nodes.begin(), nodes.end(), cmp); memset(minDistFromCentroid, 0x3f, sizeof(minDistFromCentroid)); int ans = 0; for (int u : nodes) if(getDistToSet(u) >= limit) { ans++; addToSet(u); } cout << ans; return 0; } // Lak lu theo dieu nhac ^.^

Compilation message (stderr)

catinatree.cpp: In function 'int main()':
catinatree.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |         freopen(name".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |         freopen(name".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...