Submission #529391

#TimeUsernameProblemLanguageResultExecution timeMemory
529391KoDBosses (BOI16_bosses)C++17
100 / 100
647 ms652 KiB
#include <bits/stdc++.h> using std::vector; using std::array; using std::tuple; using std::pair; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int N; std::cin >> N; vector<vector<int>> child(N); for (int i = 0; i < N; ++i) { int k; std::cin >> k; while (k--) { int p; std::cin >> p; child[p - 1].push_back(i); } } int ans = std::numeric_limits<int>::max(); for (int root = 0; root < N; ++root) { vector<int> dist(N, -1); std::queue<int> que; const auto push = [&](const int u, const int d) { if (dist[u] == -1) { dist[u] = d; que.push(u); } }; push(root, 1); while (!que.empty()) { const int u = que.front(); que.pop(); for (const int v : child[u]) { push(v, dist[u] + 1); } } int sum = 0; for (const int x : dist) { if (x == -1) { sum = -1; break; } sum += x; } if (sum != -1) { ans = std::min(ans, sum); } } std::cout << ans << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...