Submission #329589

#TimeUsernameProblemLanguageResultExecution timeMemory
329589gustasonBosses (BOI16_bosses)C++14
100 / 100
971 ms748 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int maxN = 5005; const int INF = 1e9 + 5; int n; vector<int> adj[maxN]; vector<bool> visited(maxN); int bfs(int root) { for(int i = 0; i <= n; i++) { visited[i] = false; } queue<pair<int, int>> q; q.push({root, 1}); visited[root] = true; ll cost = 0; while(!q.empty()) { auto v = q.front(); q.pop(); cost += v.second; for(int u : adj[v.first]) { if (!visited[u]) { visited[u] = true; q.push({u, v.second+1}); } } } if (count(visited.begin(), visited.end(), true) != n) return INF; return cost; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for(int ord = 1; ord <= n; ord++) { int k; cin >> k; for(int j = 0; j < k; j++) { int boss; cin >> boss; adj[boss].push_back(ord); } } int best = INF; // root the tree at every possible vertex for(int boss = 1; boss <= n; boss++) { best = min(best, bfs(boss)); } cout << best; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...