Submission #634755

#TimeUsernameProblemLanguageResultExecution timeMemory
634755tvladm2009Bosses (BOI16_bosses)C++14
100 / 100
594 ms672 KiB
#include <iostream> #include <vector> #include <queue> #include <string.h> #define int long long using namespace std; const int MAX_N = 5 * 1e3; const int INF = (1LL << 60); int dist[MAX_N + 1]; vector<int> g[MAX_N + 1]; int n; int bfs(int start) { memset(dist, 0, sizeof(dist)); queue<int> q; q.push(start); dist[start] = 1; int cost = 1; while (!q.empty()) { int u = q.front(); q.pop(); for (int v : g[u]) { if (!dist[v]) { dist[v] = dist[u] + 1; cost += dist[v]; q.push(v); } } } for (int i = 1; i <= n; i++) { if (!dist[i]) { return INF; } } return cost; } signed main() { cin >> n; for (int i = 1; i <= n; i++) { int k; cin >> k; for (int j = 1; j <= k; j++) { int x; cin >> x; g[x].push_back(i); } } int answer = INF; for (int i = 1; i <= n; i++) { answer = min(answer, bfs(i)); } cout << answer; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...