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;
vector<vector<int>> adj;
vector<int> vis;
int res = 0;
void bfs(int x){
queue<pair<int,int>> q;
q.push({x,1});
vis[x] = 1;
while(!q.empty()){
auto [a,b] = q.front();q.pop();
if(vis[a]< b) continue;
res += b;
for(auto i: adj[a]){
if(vis[i] != -1) continue;
vis[i] = b + 1;
q.push({i, vis[i]});
}
}
}
int main(){
int n; cin >> n;
adj.resize(n);
for(int i=0; i<n; i++){
int k; cin >> k;
for(int j =0; j<k; j++){
int a; cin >> a; a--;
adj[a].push_back(i);
}
}
int menor = -1;
for(int i=0; i<n; i++){
vis.assign(n,-1);
res = 0;
bfs(i);
bool ok = 1;
for(int j =0; j<n; j++) {
if(vis[j] == -1) ok = 0;
}
if(!ok) continue;
if(menor == -1) menor =res;
else menor = min(menor, res);
}
cout << menor << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |