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>
#define ll long long
#define pb push_back
using namespace std;
int main(){
ll n;
cin>>n;
vector<ll> adj[n+7];
ll k,l;
for(int i=1;i<=n;i++){
cin>>k;
for(int j=1;j<=k;j++){
cin>>l;
adj[l].pb(i);
}
}
ll ans=1e12;
for(int i=1;i<=n;i++){
ll dist[n+7];
memset(dist,0,sizeof(dist));
queue<ll> q;
q.push(i);
dist[i]=1;
while(!q.empty()){
ll y=q.front();
q.pop();
for(auto x:adj[y]){
if(!dist[x]){
dist[x]=dist[y]+1;
q.push(x);
}
}
}
bool np=false;
ll sum=0;
for(int j=1;j<=n;j++){
if(!dist[j]){
np=true;
}
else{
sum+=dist[j];
}
}
if(!np){
ans=min(ans,sum);
}
}
cout<<ans<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |