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;
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
#define all(x) x.begin(), x.end()
#define endl '\n'
const int N=5005;
const int INF=1e9;
int32_t main() {
LCBorz;
int n;cin>>n;
vector<int> adj[N];
for(int i=1;i<=n;i++){
int k;cin>>k;
for(int j=0;j<k;j++){
int p;cin>>p;
adj[p].push_back(i);
}
}
auto bfs=[&](int s)->int {
vector<int> dis(N,INF);
queue<int> q;
q.push(s);
dis[s]=0;
int ans=0,cnt=0;
while(q.size()){
int p=q.front();
q.pop();
ans+=dis[p];
cnt++;
for(int j:adj[p]){
if(dis[j]==INF){
dis[j]=dis[p]+1;
q.push(j);
}
}
}
if(cnt==n)return ans+n;
return INF;
};
int ans=INF;
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... |