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;
long long n,ans;
long long h[5005];
vector<int> v[5005];
long long solve(int p)
{
for(int i=1;i<=n;i++)
h[i]=-1;
queue<pair<int, int>> q;
q.push({p, 1});
while(!q.empty())
{
int p = q.front().first;
int len = q.front().second;
q.pop();
if(h[p] != -1)
continue;
h[p] = len;
for(int it : v[p])
if(h[it] == -1)
q.push({it, len+1});
}
long long ans=0;
for(int i=1;i<=n;i++)
{
if(h[i] == -1)
return 1e18;
ans += h[i];
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
{
int k;
cin>>k;
while(k)
{
k--;
int x;
cin>>x;
v[x].push_back(i);
}
}
long long ans=1e18;
for(int i=1;i<=n;i++)
ans=min(ans, solve(i));
cout<<ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |