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=5005;
vector<int> AdjList[maxn];
int dist[maxn];
int n;
int bfs(int s) {
//cout<<"bfs "<<s<<endl;
int cnt=0;
int ans=0;
queue<int> q;
q.push(s);
dist[s]=1;
while(!q.empty()) {
int u=q.front();
//cout<<"u = "<<u<<endl;
q.pop();
ans+=dist[u];
cnt++;
for(int v: AdjList[u]) {
//cout<<"dist "<<v<<" = "<<dist[v]<<endl;
if(!dist[v]) {
dist[v]=dist[u]+1;
//cout<<"push "<<v<<endl;
q.push(v);
}
}
}
if(cnt!=n) return 5000*5000;
else return ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n;
for(int i=1 ; i<=n ; i++) {
int k,u;
cin>>k;
for(int j=1 ; j<=k ; j++) {
cin>>u;
AdjList[u].push_back(i);
//cout<<"Adj "<<u<<" "<<i<<endl;
}
}
int ans=5000*5000;
for(int root=1 ; root<=n ; root++) {
for(int i=1 ; i<=n ; i++) dist[i]=0;
ans=min(ans,bfs(root));
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |