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 = 5001;
vector<int> g[N];
long long tmp;
bitset<N> vis;
inline int Dfs(int v, int dep = 1) {
// cout << v << '\n';
int ret = 1;
vis[v] = 1;
for (int u : g[v]) {
if (!vis[u]) {
ret += Dfs(u, dep + 1);
}
}
tmp += dep;
return ret;
}
signed main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
int k;
cin >> k;
for (int j = 0; j < k; ++j) {
int x;
cin >> x;
g[x].push_back(i);
}
}
long long ans = LLONG_MAX / 3;
for (int i = 1; i <= n; ++i) {
vis.reset();
tmp = 0;
if ( Dfs(i) == n) {
ans = min(ans, tmp);
}
// cout << '\n';
}
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... |