Submission #675066

#TimeUsernameProblemLanguageResultExecution timeMemory
675066benjaminkleynBosses (BOI16_bosses)C++17
100 / 100
638 ms644 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; int n; vector<int> children[5001]; bool vis[5001]; int dist[5001]; int main() { cin >> n; for (int i = 1, k; i <= n; i++) { cin >> k; for (int j = 0, par; j < k; j++) { cin >> par; children[par].push_back(i); } } ll ans = LLONG_MAX; for (int root = 1; root <= n; root++) { queue<int> q; q.push(root); memset(vis, false, sizeof(vis)); vis[root] = true; memset(dist, 0, sizeof(dist)); while (!q.empty()) { int u = q.front(); q.pop(); for (const int &v : children[u]) if (!vis[v]) { q.push(v); vis[v] = true; dist[v] = dist[u] + 1; } } bool connected = true; for (int i = 1; i <= n; i++) connected &= vis[i]; if (!connected) continue; ll res = 0; for (int i = 1; i <= n; i++) res += 1 + dist[i]; ans = min(ans, res); } cout << (ans == LLONG_MAX ? -1 : ans) << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...