# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
98966 | zicon35 | Bosses (BOI16_bosses) | C++14 | 0 ms | 0 KiB |
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,k,v;
vector< vector<int>> adj;
int d[20005];
int bfs(int root) {
memset(d, -1, sizeof d);
queueue<int> queue;
que.push(root);
d[root] = 1;
while(!que.empty()) {
int x = que.front();
que.pop();
for(auto i : adj[x]) {
if(d[i] == -1) {
d[i] = 1 + d[x];
que.push(i);
}
}
}
int a = 0;
for(int i = 1; i <= n; i++) {
if(d[i] == -1) return -1;
else a += d[i];
}
return a;
}
int solve(){
int k,v;
cin>>n;
adj.resize(n+2);
for(int i=1;i<=n;i++){
cin>>k;
for(int j=1;j<=k;j++){
cin>>v;
adj[v].push_back(i);
}
}
int ans = INT_MAX;
for(int i=1;i<=n;i++){
int x = bfs(i);
if(x == -1) continue;
ans = min(ans,x);
}
return ans;
}
int main(){
cout<<solve()<<endl;
return 0;
}