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> adj[5050];
bool vis[5050];
int bfs(int f){
memset(vis, 0, sizeof vis);
int res = 0, cnt = 0;
queue<pair<int, int>> q;
q.emplace(f, 1);
vis[f] = 1;
while(!q.empty()){
auto [u, d] = q.front(); q.pop();
res += d;
cnt++;
for(int v : adj[u]){
if(vis[v]) continue;
vis[v] = 1;
q.emplace(v, d + 1);
}
}
if(cnt == n) return res;
return 1e9;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for(int i = 1; i <= n; ++i){
int m; cin >> m;
while(m--){
int v; cin >> v;
adj[v].emplace_back(i);
}
}
int ans = 1e9;
for(int i = 1; i <= n; ++i){
ans = min(ans, bfs(i));
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |