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 unsigned long long
#define in pair<int, int>
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define INF (int)1e18
#define MX (int)5000+5
#define fast() ios_base::sync_with_stdio(false); cin.tie(NULL)
int n;
vector<int> poss_child[MX];
int solve(int u)
{
queue<int> bfsq;
vector<int> dist(n+1, INF);
bfsq.push(u);
dist[u] = 1;
int ans = 0;
while(bfsq.size())
{
int v = bfsq.front();
bfsq.pop();
ans+=dist[v];
for(int k: poss_child[v])
{
if(dist[k] != INF)
continue;
dist[k] = dist[v]+1;
bfsq.push(k);
}
}
for(int i = 1; i <= n; i++)
{
if(dist[i] == INF)
return INF;
}
return ans;
}
signed main()
{
fast();
cin >> n;
for(int i = 1; i <= n; i++)
{
int k;
cin >> k;
while(k--)
{
int u;
cin >> u;
poss_child[u].pb(i);
}
}
int ans = INF;
for(int i = 1; i <= n; i++)
ans = min(ans, solve(i));
cout << ans << "\n";
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... |