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;
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int N;
cin >> N;
vector<vector<int>> adj(N);
for (int i = 0; i < N; i++) {
int k;
cin >> k;
while (k--) {
int a;
cin >> a;
a--;
adj[a].emplace_back(i);
}
}
auto Solve = [&](int s) -> long long {
vector<int> dist(N, -1);
queue<int> q;
q.emplace(s);
dist[s] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto v : adj[u]) {
if (dist[v] == -1) {
dist[v] = dist[u] + 1;
q.emplace(v);
}
}
}
if (count(begin(dist), end(dist), -1) > 0) {
return 1e18;
}
long long ans = 0;
for (int i = 0; i < N; i++) {
ans += dist[i] + 1;
}
return ans;
};
long long ans = 1e18;
for (int i = 0; i < N; i++) {
ans = min(ans, Solve(i));
}
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... |