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;
const int N = 5000;
int main() {
ios::sync_with_stdio(false); cin.tie(NULL);
int n; cin >> n;
static vector<int> c[N];
for (int i = 0; i < n; i++) {
int k; cin >> k;
while (k--) {
int j; cin >> j, j--;
c[j].push_back(i);
}
}
long long ans = LLONG_MAX;
for (int s = 0; s < n; s++) {
static int d[N];
memset(d, -1, n * 4);
queue<int> q;
d[s] = 0, q.push(s);
while (!q.empty()) {
int i = q.front(); q.pop();
for (int j : c[i]) if (d[j] == -1)
d[j] = d[i] + 1, q.push(j);
}
long long x = 0; bool f = 0;
for (int i = 0; i < n; i++)
if (d[i] == -1) { f = 1; break; }
else x += d[i] + 1;
if (!f) ans = min(ans, x);
}
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... |