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;
using ll = long long;
vector<int> g[5005];
int n;
ll root (int s) {
vector<ll> dist (5005, 1e9);
dist[s] = 1;
ll res = 0;
queue<ll> q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto& v : g[u]) if (dist[u] + 1 < dist[v]) {
dist[v] = dist[u]+1;
q.push(v);
}
}
for (int i = 1; i <= n; i++) res += dist[i];
return res;
}
int main () {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
int c;
cin >> c;
while (c--) {
int u;
cin >> u;
g[u].push_back(i);
}
}
ll ans = 1e18;
for (int i = 1; i <= n; i++) ans = min(ans, root(i));
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |