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;
int visited[5002];
vector<int> graph[5002];
int iter=0;
queue<pair<int,int>> q;
int bfs(int v){
visited[v]=iter;
q.push({v,1});
int ans=0;
while(q.size()){
auto [v,d]=q.front();
q.pop();
ans+=d;
for (auto u : graph[v])if (visited[u]!=iter){
visited[u]=iter;
q.push({u,d+1});
}
}
return ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,k,v;
cin >> n;
for (int i = 1; i<=n; i++){
cin >> k;
for (int j = 1; j<=k; j++){
cin >> v;
graph[v].push_back(i);
}
}
int mn=1e9;
for (int i = 1; i<=n; i++){
iter++;
mn=min(mn,bfs(i));
}
cout << mn << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |