#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e3;
int n, h[N + 5];
bool vis[N + 5];
vector<int> ls[N + 5];
queue<int> q;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
int k;
cin >> k;
for (int j = 0; j < k; j++) {
int x;
cin >> x;
ls[x].push_back(i);
}
}
int ans = 1e18;
for (int root = 1; root <= n; root++) {
int res = 0;
memset(h, 0, sizeof(h));
memset(vis, 0, sizeof(vis));
h[root] = 1, vis[root] = true;
q.push(root);
while (q.size()) {
int u = q.front(); q.pop();
res += h[u];
for (auto&v : ls[u]) {
if (vis[v]) continue;
vis[v] = true, h[v] = h[u] + 1;
q.push(v);
}
}
bool valid = 1;
for (int i = 1; i <= n; i++) valid &= vis[i];
if (valid) ans = min(ans, res);
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
444 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
480 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
444 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
480 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
3 ms |
596 KB |
Output is correct |
14 |
Correct |
90 ms |
596 KB |
Output is correct |
15 |
Correct |
19 ms |
604 KB |
Output is correct |
16 |
Correct |
434 ms |
724 KB |
Output is correct |
17 |
Correct |
421 ms |
692 KB |
Output is correct |
18 |
Correct |
405 ms |
688 KB |
Output is correct |