# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
39871 | igzi | Bosses (BOI16_bosses) | C++14 | 680 ms | 2272 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>
#define maxN 5005
using namespace std;
vector <int> adj[maxN];
bool visited[maxN];
int n,i,x,y,ans=INT_MAX;
int bfs(int node){
queue <int> q;
int ans=0,d[maxN];
visited[node]=true;
q.push(node);
d[node]=1;
while(!q.empty()){
int c=q.front();
q.pop();
for(int i=0;i<adj[c].size();i++){
if(!visited[adj[c][i]]){
visited[adj[c][i]]=true;
q.push(adj[c][i]);
d[adj[c][i]]=d[c]+1;
}
}
}
for(int i=1;i<=n;i++){
if(!visited[i]) return -1;
ans+=d[i];
}
return ans;
}
int main()
{
cin>>n;
for(i=1;i<=n;i++){
scanf("%d", &x);
for(int j=0;j<x;j++){
scanf("%d", &y);
adj[y].push_back(i);
}
}
for(i=1;i<=n;i++){
for(int j=0;j<=n;j++){
visited[j]=false;
}
int c=bfs(i);
if(c==-1) continue;
if(c<ans) ans=c;
}
cout<<ans<<endl;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |