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 pii pair<int,int>
#define f first
#define s second
using namespace std;
const ll inf=2e9+10;
const int mxn=5e3+5;
vector<int> tree[mxn];
int n;
ll bfs(int root)
{
ll dist[mxn];
for(int i=0;i<mxn;i++)dist[i]=inf;
queue<int> q;
q.push(root);
dist[root]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i:tree[u])
{
if(dist[i]==inf)q.push(i);
dist[i]=min(dist[i],dist[u]+1);
}
}
ll mx=-1;
for(int i=1;i<=n;i++)mx=max(mx,dist[i]);
if(mx==inf)return inf;
ll cur=0;
for(int i=1;i<=n;i++)cur+=dist[i];
return cur;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
int k;
cin>>k;
for(int j=0;j<k;j++)
{
int v;
cin>>v;
tree[v].push_back(i);
}
}
ll res=inf;
for(int i=1;i<=n;i++)
{
res=min(res,bfs(i));
}
cout<<res<<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... |