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 all(x) x.begin(), x.end()
#define siz(x) int(x.size())
#define ll long long
#define ar array
#define vt vector
#define inf INT_MAX
#define lnf LLONG_MAX
const int nxm = int(5e3) + 7;
int n, k, u;
vt<vt<int>> e(nxm);
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> k;
for (int j = 0; j < k; ++j) {
cin >> u;
--u;
e[u].push_back(i);
}
}
int ans = inf;
for (int i = 0; i < n; ++i) {
//vt<vt<int>> e2(nxm);
int cnt = 0;
int tot = 0;
function<void()> bfs = [&]() {
vt<bool> vis(n, false);
queue<ar<int, 2>> que;
que.push({i, 1});
while (que.size()) {
u = que.front()[0];
int p = que.front()[1];
que.pop();
if (vis[u]) {
continue;
}
tot += p;
++cnt;
//if (p != -1) {
//e2[p].push_back(u);
//}
vis[u] = true;
for (int v : e[u]) {
if (!vis[v]) {
que.push({v, p + 1});
}
}
}
};
bfs();
if (cnt != n) {
continue;
}
//vt<int> d(n, 0);
//function<void(int)> dfs = [&](int root) {
//int sum = 0;
//for (int v : e2[root]) {
//dfs(v);
//sum += d[v];
//}
//d[root] = sum + 1;
//tot += d[root];
//};
//dfs(i);
ans = min(ans, tot);
}
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... |