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 MAXN 5010
long long n;
vector<long long> adj[MAXN];
long long dist[MAXN];
queue<long long> bfsq;
void bfs(long long pret)
{
for (long long i=1;i<=n;i++) dist[i]=LLONG_MAX;
dist[pret]=1;bfsq.push(pret);
while (bfsq.empty()==false)
{
long long node=bfsq.front();
bfsq.pop();
for (long long sled:adj[node])
{
if (dist[sled]!=LLONG_MAX) continue;
dist[sled]=dist[node]+1;
bfsq.push(sled);
}
}
}
int main()
{
cin>>n;
for (long long i=1;i<=n;i++)
{
long long s;cin>>s;
for (long long j=0;j<s;j++)
{
long long x;cin>>x;
adj[x].push_back(i);
}
}
long long ans=LLONG_MAX;
for (long long i=1;i<=n;i++)
{
bfs(i);long long sol=0;
for (long long j=1;j<=n;j++) sol+=dist[j];
ans=min(ans,sol);
}
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... |