#include "bits/stdc++.h"
#define int long long
using namespace std;
const int inf = 1e18 + 18;
const int sz = 5e3 + 15;
vector<int> aj[sz];
bool vi[sz];
int bfs(int x) {
fill_n(vi, sz, false);
queue<pair<int,int>> q;
q.push(make_pair(1,x));
int r = 0;
while (!q.empty()) {
auto tp = q.front();
q.pop();
int d = tp.first;
int v = tp.second;
if (vi[v])
continue;
vi[v] = true;
r += d;
// cout << v << ' ' << d << '\n';
for (int u : aj[v])
if (!vi[u])
q.push(make_pair(d + 1, u));
}
return r;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
for (int i = 1; i <= n; i ++) {
int k;
cin >> k;
for (int j = 1; j <= k; j ++) {
int v;
cin >> v;
aj[v].push_back(i);
}
}
int mn = inf;
for (int i = 1; i <= n; i ++) {
int r = bfs(i);
mn = min(mn, r);
// cout << i << ' ' << r << '\n';
}
cout << mn << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |