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 ep emplace
#define epb emplace_back
#define int long long
using namespace std;
int n,va[5007],ans=LLONG_MAX;
vector<int> adj[5007];
bool visited[5007];
void bfs(int node)
{
queue<int> q;
q.ep(node);
while(!q.empty())
{
auto u=q.front();
q.pop();
for(auto v:adj[u])
{
if(visited[v]) continue;
visited[v]=true;
va[v]=va[u]+1;
q.ep(v);
}
}
}
signed main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
int num;
cin>>num;
while(num--)
{
int v;
cin>>v;
adj[v].epb(i);
}
}
for(int i=1;i<=n;i++)
{
memset(visited,false,sizeof(visited));
memset(va,0,sizeof(va));
visited[i]=true;
va[i]=1;
bfs(i);
int value=0;
for(int j=1;j<=n;j++)
{
if(!visited[i])
{
value=LLONG_MAX;
break;
}
value+=va[j];
}
// cout<<value<<"\n";
ans=min(ans,value);
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |