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;
#define ll long long
#define f first
#define s second
#define pb push_back
#define pi pair<int,int>
#define pl pair<ll,int>
const int MAX = 5001;
int color[MAX];
ll dis[MAX];
vector<int>g[MAX];
int n;
ll bfs(int u){
fill(dis+1,dis+1+n, LLONG_MAX);
dis[u] =1;
queue<int>q;
q.push(u);
while(!q.empty()){
int from = q.front();
q.pop();
// if(color[from]) continue;
//color[from]=1;
for(int to: g[from]){
if(dis[to]>dis[from]+1){
dis[to]= dis[from]+1;
q.push(to);
}
}
}
ll sum = 0;
for(int i = 1; i <=n; i++) {
sum+= dis[i];
if(dis[i]==LLONG_MAX) return LLONG_MAX;
}
// cout << endl << sum << " "<< u << endl;
// cout << u << " "<< sum << endl;
return sum;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
int temp, k;
for(int i = 1; i <= n; i++){
cin >> temp;
while(temp--){
cin >> k;
g[k].pb(i);
}
}
ll ans= LLONG_MAX;
for(int i= 1; i <= n; i++){
ans = min(bfs(i),ans);
}
cout << ans << 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... |