Submission #347202

#TimeUsernameProblemLanguageResultExecution timeMemory
347202AmShZCat in a tree (BOI17_catinatree)C++11
100 / 100
510 ms54764 KiB
// khodaya khodet komak kon //# pragma GCC target ("avx2") //# pragma GCC optimize("Ofast") //# pragma GCC optimize("unroll-loops") //# pragma GCC target("avx,avx2,sse,sse2,fma") # pragma comment(linker, "/stack:200000000") # pragma GCC optimize("Ofast") # include <bits/stdc++.h> /* // ordered_set # include <ext/pb_ds/assoc_container.hpp> # include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; # define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> */ using namespace std; typedef long long ll; typedef long double ld; typedef pair <int, int> pii; typedef pair <pii, int> ppi; typedef pair <int, pii> pip; typedef pair <pii, pii> ppp; typedef pair <ll, ll> pll; # define A first # define B second # define endl '\n' # define sep ' ' # define all(x) x.begin(), x.end() # define kill(x) return cout << x << endl, 0 # define SZ(x) int(x.size()) # define lc id << 1 # define rc id << 1 | 1 # define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0); ll power(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2, md) % md : power(a * a % md, b / 2, md) % md));} const int xn = 2e5 + 10; const int xm = 10 + 10; const int sq = 10; const int inf = 1e9 + 10; const ll INF = 1e18 + 10; const int mod = 998244353; const int base = 257; int n, sz[xn], H[xn], ord[xn]; int f[xn], ans, d, ptr[xn]; pii rts[xn][xm]; vector <int> adj[xn]; bool hide[xn]; void plant(int v, int p = - 1){ sz[v] = 1; for (int u : adj[v]) if (u != p && !hide[u]) plant(u, v), sz[v] += sz[u]; } void add(int v, int root, int h = 0, int p = - 1){ rts[v][ptr[v] ++] = {root, h}; for (int u : adj[v]) if (!hide[u] && u != p) add(u, root, h + 1, v); } int find_centroid(int v, int s, int p = - 1){ for (int u : adj[v]) if (!hide[u] && u != p && sz[u] + sz[u] > s) return find_centroid(u, s, v); return v; } void solve(int v){ plant(v); v = find_centroid(v, sz[v]); f[v] = inf, add(v, v); hide[v] = true; for (int u : adj[v]) if (!hide[u]) solve(u); } bool cmp(int v, int u){ return H[v] > H[u];} int main(){ InTheNameOfGod; cin >> n >> d; for (int i = 1; i < n; ++ i){ int par; cin >> par; adj[par].push_back(i); adj[i].push_back(par); H[i] = H[par] + 1; ord[i] = i; } solve(0); sort(ord, ord + n, cmp); for (int i = 0; i < n; ++ i){ int v = ord[i]; bool flag = false; for (int j = 0; j < ptr[v]; ++ j) flag |= rts[v][j].B + f[rts[v][j].A] < d; if (flag) continue; ++ ans; for (int j = 0; j < ptr[v]; ++ j) f[rts[v][j].A] = min(f[rts[v][j].A], rts[v][j].B); } cout << ans << endl; return 0; }

Compilation message (stderr)

catinatree.cpp:6: warning: ignoring #pragma comment  [-Wunknown-pragmas]
    6 | # pragma comment(linker, "/stack:200000000")
      |
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...