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;
using ll=long long;
const int maxn=5001;
int n;
vector<int> g[maxn];
ll bfs(int s){
queue<int> q;
ll d[maxn];
fill(d, d+n+1, 0);
d[s] = 1;
q.push(s);
ll sum = 0, cnt = 1;
while(!q.empty()){
int x = q.front();
q.pop();
sum += d[x];
cnt++;
for(int i : g[x]){
if(!d[i]){
d[i] = d[x] + 1;
q.push(i);
}
}
}
return cnt == n ? sum : LLONG_MAX;
}
int main()
{
cin >> n;
for(int i=1;i<=n;i++){
int k;
cin>>k;
for(int j=0;j<k;j++) {
int x;
cin>>x;
g[x].push_back(i);
}
}
ll ans=LLONG_MAX;
for(int i=1;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... |