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 N;
vector<int> adjList[5001];
int bfs(int root) {
vector<bool> seen(N+1);
queue<pair<int, int>> q; // node, depth
q.push({root, 1}); // starting depth at one
int ans = 0;
while (!q.empty()) {
pair<int, int> cur = q.front();
int u = cur.first;
int depth = cur.second;
q.pop();
if (!seen[u]) {
seen[u] = true;
ans += depth;
for (auto v: adjList[u]) {
if (!seen[v]) {
q.push({v, depth+1});
}
}
}
}
return ans;
}
int main() {
cin >> N;
for (int v = 1; v <= N; v++) {
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int u;
cin >> u;
adjList[u].push_back(v);
}
}
int ans = INT_MAX;
for (int i = 1; i <= N; i++) {
ans = min(ans, bfs(i));
}
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... |