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 int long long int
vector< vector<int> > v;
int n;
int bfs(int r)
{
queue<int> q;
q.push(r);
vector<int> a(n);
vector<int> vis(n);
vis[r] = 1;
a[r] = 1;
while(!q.empty())
{
int cur = q.front();
q.pop();
for(int i: v[cur])
{
if(!vis[i])
{
vis[i] = 1;
a[i] += a[cur]+1;
q.push(i);
}
}
}
for(int i = 0;i<n;i++)
if(!vis[i])
return INT_MAX;
return accumulate(a.begin(), a.end(), 0ll);
}
#undef int
int main()
{
#define int long long int
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
v.resize(n);
for(int i = 0;i<n;i++)
{
int k;
cin>>k;
for(int j = 0;j<k;j++)
{
int x;
cin>>x;
x--;
v[x].push_back(i);
}
}
int ans = INT_MAX;
for(int i = 0;i<n;i++)
ans = min(ans, bfs(i));
cout<<ans<<endl;
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... |