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 pb push_back
using namespace std;
typedef vector<int> vi;
typedef long long LL;
const LL LINF = 1e18;
const int mxsz = 5e3 + 5;
int n;
vi adj[mxsz];
queue<int> q;
int dep[mxsz], ord[mxsz], lvl[mxsz];
LL test(int rt){
	while (!q.empty()) q.pop();
	fill(dep+1, dep+n+1, 0x3f3f3f3f);
	fill(lvl, lvl+n+1, 0);
	dep[rt] = 0;
	q.push(rt);
	while (!q.empty()){
		int u = q.front(); q.pop();
		for (int nx : adj[u]){
			if (dep[nx] > dep[u] + 1){
				dep[nx] = dep[u] + 1;
				q.push(nx);
			}
		}
	}
	int mx = 0;
	for (int i = 1; i <= n; i++) mx = max(mx, dep[i]);
	if (mx > n) return LINF;
	for (int i = 1; i <= n; i++) lvl[dep[i]]++;
	LL tot = 0, p = 0;
	for (int i = n; i >= 0; --i){
		tot += lvl[i];
		tot += p;
		p += lvl[i];
	}
	return tot;
}
void read(){
	cin >> n;
	for (int i = 1, t; i <= n; i++){
		cin >> t;
		for (int j = 0, u; j < t; j++){
			cin >> u;
			adj[u].pb(i);
		}
		ord[i] = i;
	}
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	read();
	LL mn = LINF;
	for (int i = 1; i <= n; i++){
		LL cur = test(i);
		mn = min(mn, cur);
	}
	cout << mn << '\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... |