제출 #1058716

#제출 시각아이디문제언어결과실행 시간메모리
1058716gygCat in a tree (BOI17_catinatree)C++17
0 / 100
1 ms2652 KiB
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; const int MAX_N = 1500 + 5, INF = 1e9; int n, k; array<vector<int>, MAX_N> adj; /************************************/ array<array<int, MAX_N>, MAX_N> floyd; void precomp() { for (int u = 1; u <= n; u++) { fill(floyd[u].begin(), floyd[u].end(), INF); for (int v : adj[u]) floyd[u][v] = 1; floyd[u][u] = 0; } for (int u = 1; u <= n; u++) for (int v = 1; v <= n; v++) for (int w = 1; w <= n; w++) floyd[u][v] = min(floyd[u][v], floyd[u][w] + floyd[w][v]); } /************************************/ array<int, MAX_N> siz; array<bool, MAX_N> del; void update_siz(int u, int par = -1) { siz[u] = 1; for (int v : adj[u]) { if (v == par) continue; if (del[v]) continue; update_siz(v, u), siz[u] += siz[v]; } } int find_cent(int u, int tot_siz, int par = -1) { for (int v : adj[u]) { if (v == par) continue; if (del[v]) continue; if (siz[v] * 2 > tot_siz) return find_cent(v, tot_siz, u); } return u; } array<vector<int>, MAX_N> ancs; // Reversed array<unordered_map<int, int>, MAX_N> dist; array<unordered_map<int, vector<pii>>, MAX_N> dists; // [itself] = whole thing void find_dists(int u, int d, int par, int cent, int sub) { dist[cent][u] = d, dists[cent][sub].push_back({d, u}); ancs[u].push_back(cent); for (int v : adj[u]) { if (v == par) continue; if (del[v]) continue; find_dists(v, d + 1, u, cent, sub); } } void build(int u = 1) { update_siz(u); int cent = find_cent(u, siz[u]); dist[cent][cent] = 0, dists[cent][cent].push_back({0, cent}); ancs[cent].push_back(cent); for (int v : adj[cent]) { if (del[v]) continue; find_dists(v, 1, u, cent, v); sort(dists[cent][v].begin(), dists[cent][v].end()); dists[cent][cent].insert(dists[cent][cent].begin(), dists[cent][v].begin(), dists[cent][v].end()); } sort(dists[cent][cent].begin(), dists[cent][cent].end()); del[cent] = true; for (int v : adj[cent]) if (!del[v]) build(v); } int mini_query(int u, int d, int exc) { int i = lower_bound(dists[u][u].begin(), dists[u][u].end(), make_pair(d, -1)) - 1 - dists[u][u].begin(); // First index with dist < d int j = (exc == -1) ? -1 : lower_bound(dists[u][exc].begin(), dists[u][exc].end(), make_pair(d, -1)) - 1 - dists[u][exc].begin(); int ans = i + 1 - (j + 1); return ans; } int query(int u) { reverse(ancs[u].begin(), ancs[u].end()); int ans = 0; for (int i = 0; i < ancs[u].size(); i++) { int v = ancs[u][i]; if (i == 0) ans += mini_query(v, k, -1); else ans += mini_query(v, k - dist[v][u], ancs[u][i - 1]); } // cout << u << ": " << ans << endl; return ans; } vector<pii> order; array<bool, MAX_N> used; int comp() { for (int u = 1; u <= n; u++) order.push_back({query(u), u}); sort(order.begin(), order.end()); int ans = 0; for (pii x : order) { int u = x.second; if (used[u]) continue; ans++; for (int v = 1; v <= n; v++) if (floyd[u][v] < k) used[v] = true; } return ans; } int main() { // freopen("cat.in", "r", stdin); cin >> n >> k; assert(n <= 1500); for (int u = 2; u <= n; u++) { int v; cin >> v; v++; adj[u].push_back(v), adj[v].push_back(u); } precomp(); build(); int ans = comp(); cout << ans << '\n'; }

컴파일 시 표준 에러 (stderr) 메시지

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