Submission #857765

#TimeUsernameProblemLanguageResultExecution timeMemory
857765qrnoBosses (BOI16_bosses)C++14
100 / 100
402 ms1012 KiB
#include <bits/stdc++.h> using namespace std; const int MAXN = 5e3; int best = -1; int N; array<vector<int>, MAXN> G; array<int, MAXN> val, last; int bfs(int s) { queue<int> Q; val[s] = 1; last[s] = s; Q.push(s); int done = 0, sum = 0; while (!Q.empty()) { auto v = Q.front(); Q.pop(); sum += val[v]; if (best != -1 && sum >= best) return -1; done++; for (auto u : G[v]) { if (last[u] != s) { val[u] = val[v]+1; last[u] = s; Q.push(u); } } } if (done < N) return -1; return sum; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N; for (int i = 0; i < N; i++) { int K; cin >> K; while (K--) { int u; cin >> u; u--; G[u].push_back(i); } } for (int i = 0; i < N; i++) { int sum = bfs(i); if (sum != -1 && (best == -1 || best > sum)) best = sum; } cout << best << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...