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;
int n, ans, dist[5002], k, x;
vector<int>G[5002];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> k;
while (k--) {
cin >> x;
G[x].push_back(i);
}
}
ans = 1e9;
for (int i = 1; i <= n; i++) {
memset(dist, -1, sizeof(dist));
dist[i] = 1;
queue<int>q;
q.push(i);
int cost = 0, count = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
cost += dist[x];
count++;
for (auto it : G[x])
if (dist[it] == -1) {
dist[it] = dist[x] + 1;
q.push(it);
}
}
if (count == n)
ans = min(ans, cost);
}
cout << ans;
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... |