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;
#define int long long
const int INF = 0x3f3f3f3f;
const int MAXN = 5010;
vector<int> adj[MAXN];
int32_t main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
while (x--) {
int a;
cin >> a;
adj[a].push_back(i);
}
}
int ans = 0x3f3f3f3f3f3f3f3f;
for (int i = 1; i <= n; i++) {
vector<int> dist(n + 1, INF);
vector<bool> mark(n + 1);
dist[i] = 1;
mark[i] = 1;
queue<int> q;
q.push(i);
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto u : adj[v]) {
if (mark[u]) continue;
mark[u] = 1;
dist[u] = dist[v] + 1;
q.push(u);
}
}
int sum = 0;
for (int i = 1; i <= n; i++) sum += dist[i];
ans = min(ans, sum);
}
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... |