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;
using ll = long long;
const int maxn=5e3+5;
int n;
int dis[maxn];
vector<int>E[maxn];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1;i <= n;i++){
int sz; cin >> sz;
for(int j = 0;j < sz;j++){
int p; cin >> p;
E[p].push_back(i);
}
}
queue<int>q;
int ans = 1e9;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++) dis[j] = 1e9;
dis[i] = 1;
q.push(i);
while(!q.empty()){
int x = q.front(); q.pop();
for(auto j : E[x]){
if(dis[j] > dis[x] + 1){
dis[j] = dis[x] + 1;
q.push(j);
}
}
}
bool tr = 0;
int nw = 0;
for(int j = 1;j <= n;j++){
if(dis[j] == 1e9){
tr = 1;
break;
}
nw += dis[j];
}
if(!tr) ans = min(ans,nw);
}
cout<<ans;
}
/*
4
1 4
3 1 3 4
2 1 2
1 3
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |