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 INF = 1e9;
const int MAXN = 5e3;
int N;
vector<int> children[MAXN + 5];
int vis[MAXN + 5];
queue<int> BFS;
int bfs(int start) {
while (!BFS.empty()) BFS.pop();
memset(vis, -1, sizeof vis);
BFS.push(start);
vis[start] = 1;
while (!BFS.empty()) {
int cur = BFS.front(); BFS.pop();
for (auto next: children[cur]) {
if (vis[next] != -1) continue;
vis[next] = vis[cur] + 1;
BFS.push(next);
}
}
int ans = 0;
for (int i = 0; i < N; i++) {
if (vis[i] == -1) return INF;
ans += vis[i];
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> N;
for (int i = 0; i < N; i++) {
int sz; cin >> sz;
while (sz--) {
int u; cin >> u; u--;
children[u].push_back(i);
}
}
int ans = INF;
for (int i = 0; i < N; i++) ans = min(ans, bfs(i));
cout << ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |