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 N = 5e3 + 5;
int dis[N], res, cnt, n, ans = 1e9;
vector <int> g[N];
void BFS(int v) {
memset(dis, 31, sizeof dis), res = cnt = 0;
queue <int> q;
dis[v] = 0, q.push(v);
while (!q.empty()) {
v = q.front(), q.pop();
res += dis[v] + 1, cnt++;
for (int u : g[v]) if (dis[v] + 1 < dis[u]) dis[u] = dis[v] + 1, q.push(u);
}
if (cnt == n) ans = min(ans, res);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
int k; cin >> k;
for (int j = 0; j < k; j++) {
int u; cin >> u;
g[u].push_back(i);
}
}
for (int i = 1; i <= n; i++) BFS(i);
cout << ans << "\n";
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... |