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;
const int N = 5005, INF = 1e9;
int n, d[N];
int ans = INF;
vector < int > g[N];
inline void bfs(int rt){
for(int i = 1; i <= n; i++){
d[i] = INF;
}
d[rt] = 1;
queue < int > q;
q.push(rt);
while(!q.empty()){
int v = q.front();
q.pop();
for(int to : g[v]){
if(d[to] > d[v] + 1){
d[to] = d[v] + 1;
q.push(to);
}
}
}
int sum = 0;
for(int i = 1; i <= n; i++){
if(d[i] == INF){
return;
}
sum += d[i];
}
//cout << rt << " " << sum << "\n";
ans = min(ans, sum);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
int x, y;
cin >> x;
for(int j = 1; j <= x; j++){
cin >> y;
g[y].push_back(i);
}
}
for(int i = 1; i <= n; i++){
bfs(i);
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |