# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
159549 | InfiniteJest | 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 <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <math.h>
#include <map>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
ifstream in("input.txt");
ofstream out("output.txt");
typedef long long ll;
int fi(){
char g=getchar_unlocked();
while(g<'0'||g>'9')g=getchar_unlocked();
int num=0;
while(g>='0'&&g<='9'){
num=(num<<1)+(num<<3)+g-48;
g=getchar_unlocked();
}
return num;
}
int n;
vector<int> nodi[5001];
vector<int> p[5001];
bool vis[5001];
int minn=1e9;
int val[5001];
int f;
int sum=0;
void bfs(int k){
queue<pair<int,int> > q;
q.push(mp(k,1));
vis[k]=1;
while(!q.empty()){
int cur=q.front.fi();
f++;
int depth=q.front().se;
sum+=q.front().se;
q.pop();
for(int i=0;i<nodi[cur].size();i++){
if(vis[nodi[cur][i]])continue;
vis[nodi[cur][i]]=1;
q.push(mp(nodi[cur][i],depth));
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
n=fi();
for(int i=0;i<n;i++){
int k; k=fi();
for(int y=0;y<k;y++){
int a; a=fi();
nodi[a-1].pb(i);
}
}
for(int i=0;i<n;i++){
for(int y=0;y<n;y++){
p[y].clear();
vis[y]=0;
val[y]=0;
}
f=0;
bfs(i);
if(f!=n)continue;
minn=min(minn,sum);
//cout<<"i "<<i<<" v "<<sum<<endl;
}
cout<<minn;
}