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;
const int maxN = 5005;
const int INF = 1e9 + 5;
int n;
vector<int> adj[maxN];
vector<bool> visited(maxN);
int bfs(int root) {
for(int i = 0; i <= n; i++) {
visited[i] = false;
}
queue<pair<int, int>> q;
q.push({root, 1});
visited[root] = true;
ll cost = 0;
while(!q.empty()) {
auto v = q.front();
q.pop();
cost += v.second;
for(int u : adj[v.first]) {
if (!visited[u]) {
visited[u] = true;
q.push({u, v.second+1});
}
}
}
if (count(visited.begin(), visited.end(), true) != n) return INF;
return cost;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for(int ord = 1; ord <= n; ord++) {
int k;
cin >> k;
for(int j = 0; j < k; j++) {
int boss;
cin >> boss;
adj[boss].push_back(ord);
}
}
int best = INF;
// root the tree at every possible vertex
for(int boss = 1; boss <= n; boss++) {
best = min(best, bfs(boss));
}
cout << best;
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... |