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;
using ll = long long;
constexpr int N = 1e5+5;
int n;
vector<int> adj[N];
ll solve(int root) {
vector<ll> d(n, 1e9);
d[root] = 1;
queue<int> q;
q.push(root);
while(!q.empty()) {
int now = q.front();
q.pop();
for(int v : adj[now]) {
if(d[v] > d[now] + 1) {
d[v] = d[now] + 1;
q.push(v);
}
}
}
ll ret = 0;
for(int i = 0; i < n; i++) {
ret += d[i];
}
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 0; i < n; i++) {
int k;
cin >> k;
for(int j = 0; j < k; j++) {
int x;
cin >> x;
x--;
adj[x].push_back(i);
}
}
ll ans = 1e9;
for(int i = 0; i < n; i++) {
ans = min(ans, solve(i));
}
assert(ans != 1e9);
cout << ans;
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... |