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 foreach for
#define in :
using namespace std;
typedef long long ll;
/*
Konichiwa
Konichiwa
Ara ~~ ara
Bob no taisuki - Shinobu Kocho
    * * * * *
  *          *
 *         *
*         *        I love SHINOBU <3 <3 <3
 *         *
  *          *
    * * * * *
*/
/*
_________________________
    Author : Bob15324
_________________________
*/
template<class X , class Y>
    bool Minimize(X & x , Y y)
    {
        if(x == -1 || x >y)
        {
            x = y;
            return true;
        }
        return false;
    }
template<class X , class Y>
    bool Maximize(X & x , Y y)
    {
        if( x < y)
        {
            x = y;
            return true;
        }
        return false;
    }
/* End of templates. Let's see what do we have here */
const int N = 5e3+1;
int n;
vector<vector<int>>vertices , adj;
bool space[N];
int dist[N];
void DFS(int u ,int daddy)
{
    dist[u] = 1;
    foreach(int v in adj[u])
    {
        if(v == daddy)
        {
            continue;
        }
        DFS(v , u);
        dist[u] += dist[v];
    }
}
int k;
int main()
{
    ios_base :: sync_with_stdio(0);cin.tie(0);
    cin >> n ;
    vertices.resize(n+1);
    adj.resize(n+1);
    for(int i = 1; i <= n ; i++)
    {
        cin >> k;
        while(k--)
        {
            int u;
            cin >> u;
            vertices[u].push_back(i);
        }
    }
    int res = -1;
    for(int root = 1;  root  <= n ; root++)
    {
        for(int i = 1; i <= n ; i++)
        {
            space[i] = false;
            adj[i].clear();
        }
        space[root] = true;
        queue<int>q;
        q.push(root);
        int Left = n;
        while(!q.empty())
        {
            int u = q.front();
            q.pop();
            Left--;
            foreach(int v in vertices[u])
            {
                if(space[v])
                {
                    continue;
                }
                space[v] = true;
                q.push(v);
                adj[u].push_back(v);
            }
        }
        if(Left > 0)
        {
            continue;
        }
        DFS(root , -1);
        int sum = 0;
        for(int i =1; i <= n ; i++)
        {
            sum += dist[i];
        }
        Minimize(res , sum);
    }
    cout << res;
    return 0;
}
//Ehem. My code is amazing. Pay me 2$.Thank you.
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |