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 N=5005;
int n,d[N];
vector<int>adj[N];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n;
for(int i=1;i<=n;i++)
{
int m;
cin>>m;
while(m--)
{
int p;
cin>>p;
adj[p].push_back(i);
}
}
long long res=1e18;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++) d[j]=-1;
queue<int>pq;
d[i]=1;
pq.push(i);
while(!pq.empty())
{
int u=pq.front();
pq.pop();
for(auto&v:adj[u])
{
if(d[v]==-1)
{
d[v]=d[u]+1;
pq.push(v);
}
}
}
bool check=true;
for(int j=1;j<=n;j++) if(d[j]==-1) check=false;
if(check)
{
long long cur=0;
for(int j=1;j<=n;j++) cur+=d[j];
res=min(res,cur);
}
}
cout<<res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |