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>
using namespace std;
vector<int> boss[5001];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n; cin >> n;
for (int i = 1; i <= n; i++) {
int k; cin >> k;
for (int j = 0; j < k; j++) {
int b; cin >> b;
boss[b].push_back(i);
}
}
int ans = 1e9;
for (int i = 1; i <= n; i++) {
vector<bool> vis(n + 1);
int nodes = 0, sum = 0;
queue<pair<int, int>> q;
q.push({i, 1});
while (!q.empty()) {
int cur = q.front().first, dep = q.front().second;
q.pop();
if (vis[cur]) continue;
vis[cur] = true;
nodes++;
sum += dep;
for (int b : boss[cur]) {
q.push({b, dep + 1});
}
}
if (nodes == n) {
// cerr << "D " << i << ' ' << sum << '\n';
ans = min(ans, sum);
}
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |