#include <bits/stdc++.h>
using namespace std;
void fastIO(){ios_base::sync_with_stdio(false),cin.tie(0);}
#define int long long
const int INF = (int)1e18;
const int MAXV = 5020;
int V;
vector<int> adj[MAXV];
int ans = INF;
int BFS (int root) {
deque<int> DQ;
DQ.push_back(root);
vector<int> len(V, INF);
len[root] = 1;
while (!DQ.empty()) {
int nodeOn = DQ.front();
DQ.pop_front();
for (int nodeNxt : adj[nodeOn]) {
if (len[nodeNxt] > len[nodeOn] + 1) {
len[nodeNxt] = len[nodeOn] + 1;
DQ.push_back(nodeNxt);
}
}
}
int res = 0;
for (int i = 0; i < V; i++) {
res += len[i];
}
return res;
}
signed main() {
fastIO();
cin >> V;
for (int i = 0; i < V; i++) {
int num;
cin >> num;
for (int j = 0; j < num; j++) {
int v;
cin >> v;
v--;
adj[v].push_back(i);
}
}
for (int root = 0; root < V; root++) {
ans = min(ans, BFS(root));
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |