Submission #1112945

#TimeUsernameProblemLanguageResultExecution timeMemory
1112945ljtunasCat in a tree (BOI17_catinatree)C++17
0 / 100
10 ms29264 KiB
// author : anhtun_hi , nqg #include <bits/stdc++.h> #define ll long long #define ii pair<ll, ll> #define fi first #define se second #define all(x) x.begin(), x.end() #define reset(h, v) memset(h, v, sizeof h) #define Forv(i, a) for(auto& i : a) #define For(i, a, b) for(int i = a; i <= b; ++i) #define Ford(i, a, b) for(int i = a; i >= b; --i) #define c_bit(i) __builtin_popcountll(i) #define Bit(mask, i) ((mask >> i) & 1LL) #define onbit(mask, i) ((mask) bitor (1LL << i)) #define offbit(mask, i) ((mask) &~ (1LL << i)) #define TIME (1.0 * clock() / CLOCKS_PER_SEC) using namespace std; namespace std { template <typename T, int D> struct _vector : public vector <_vector <T, D - 1>> { static_assert(D >= 1, "Dimension must be positive!"); template <typename... Args> _vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {} }; template <typename T> struct _vector <T, 1> : public vector <T> { _vector(int n = 0, const T& val = T()) : vector <T> (n, val) {} }; template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;} template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;} } const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5; const int MAXN = 1e6 + 5; const ll mod = 1e9 + 7; const ll oo = 1e18; //#define int long long int n, d, sz[MAXN], del[MAXN], dep[MAXN]; vector<int> g[MAXN]; int p[MAXN][22], dad[MAXN]; void dfs(int u, int par) { p[u][0] = par; For(k, 1, 20) p[u][k] = p[p[u][k - 1]][k - 1]; for(int v : g[u]) { if (v == par) continue; dep[v] = dep[u] + 1; dfs(v, u); } } int getlca(int u, int v) { if (dep[u] < dep[v]) swap(u, v); int len = dep[u] - dep[v]; For(k, 0, 20) if (Bit(len, k)) u = p[u][k]; if (u == v) return u; Ford(k, 20, 0) if (p[u][k] != p[v][k]) u = p[u][k], v = p[v][k]; return p[u][0]; } int dist(int u, int v) { return dep[u] + dep[v] - 2 * dep[getlca(u, v)]; } void dfsSize(int u){ sz[u] = 1; Forv(v, g[u]) if(!del[v]) dfsSize(v), sz[u] += sz[v]; } int find(int u, int n){ Forv(v, g[u]) if(!del[v] && sz[v] > n / 2) return find(v, n); return u; } void buildCentroid(int u, int par){ dfsSize(u); int centroid = find(u, sz[u]); del[centroid] = true; dad[centroid] = par; Forv(v, g[u]) if(!del[v]) buildCentroid(v, centroid); } int mn[MAXN]; int get(int u){ int res = inf, v = u; while(v){ minimize(res, mn[v] + dist(u, v)); v = dad[v]; } return res; } void update(int u){ int v = u; while(v){ minimize(mn[v], dist(u, v)); v = dad[v]; } } void Solve() { cin >> n >> d; For(i, 2, n){ int p; cin >> p; ++p; g[p].push_back(i); } buildCentroid(1, 0); dfs(1, 0); reset(mn, 0x3f); vector<int> List; For(i, 1, n) List.push_back(i); sort(all(List), [&](int u, int v){ return dep[u] > dep[v]; }); int ans = 0; Forv(i, List){ if(get(i) >= d){ ++ans; update(i); } } cout << ans << '\n'; } signed main() { cin.tie(0) -> sync_with_stdio(0); if(fopen("BOI17_catinatree.inp", "r")) { freopen("BOI17_catinatree.inp", "r", stdin); freopen("BOI17_catinatree.out", "w", stdout); } int t = 1; // cin >> t; while(t --) Solve(); return 0; }

Compilation message (stderr)

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