#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(),(x).end()
const int maxn = 1e6 + 10;
//const int maxs = 9;
const int inf = 1e9 + 10;
const int mod = 998244353;
int n, dis[maxn];
vector<int> G[maxn];
ll bfs(int s) {
ll ret = 0;
ll cnt = 0;
queue<int> q;
for (int i = 1; i <= n; i++) {
dis[i] = inf;
}
dis[s] = 0;
q.push(s);
while (!q.empty()) {
auto v = q.front();
q.pop();
cnt++;
ret += dis[v];
for (int u : G[v]) {
if (dis[u] > dis[v] + 1) {
dis[u] = dis[v] + 1;
q.push(u);
}
}
}
if (cnt != n)
return inf;
return ret + n;
}
signed main() {
cin >> n;
for (int v = 1; v <= n; v++) {
int k; cin >> k;
for (int j = 1; j <= k; j++) {
int u; cin >> u;
G[u].pb(v);
}
}
ll ans = inf;
for (int rt = 1; rt <= n; rt++) {
ans = min(ans, bfs(rt));
}
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... |