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;
typedef long long ll;
const int mxN = 5e4+10;
int n;
vector<int> adj[mxN];
int bfs(int st) {
vector<bool> vis(n+10, 0);
queue<pair<int ,int>> q;
q.push({1, st});
int ret = 0;
vis[st] = 1;
while(q.size()) {
auto node = q.front();
ret += node.first;
vis[node.second] = 1;
q.pop();
for (auto it : adj[node.second]) {
if (!vis[it]) {
q.push({node.first+1, it});
}
}
}
return ret;
}
void solve() {
cin >> n;
for (int u = 1; u <= n; u++) {
int k;
cin >> k;
for (int j = 0; j < k; j++) {
int v;
cin >> v;
adj[v].push_back(u);
}
}
int ans = INT_MAX;
for (int i = 1; i <= n; i++) {
ans = min(ans, bfs(i));
}
cout << ans;
}
int main() {
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |