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>
#define F first
#define S second
#define forn(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cout << setprecision(12);
cin.tie(nullptr);
int n;
cin >> n;
vector<vector<int>> adj(n);
forn(v, n) {
int k; cin >> k;
forn(i, k) {
int u; cin >> u; u--;
adj[u].push_back(v);
}
}
ll ans = LLONG_MAX;
queue<int> q;
vector<int> depth;
vector<vector<int>> tree;
forn(root, n) {
depth.assign(n, -1);
tree.assign(n, {});
q.push(root);
depth[root] = 1;
int num_visited = 1;
while (!q.empty()) {
int v = q.front(); q.pop();
for (int w : adj[v]) {
if (depth[w] != -1)
continue;
tree[v].push_back(w);
q.push(w);
depth[w] = depth[v] + 1;
num_visited++;
}
}
if (num_visited < n)
continue;
ll total = 0;
for (ll d : depth)
total += d;
ans = min(ans, total);
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |