This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |