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>
#define S second
#define F first
using namespace std;
typedef long long int ll;
typedef pair<int,int> pii;
const int maxn=5050;
int inf = 1e9;
vector<int> adj[maxn];
queue<int> q;
int mark[maxn];
int dist[maxn];
ll ans = inf;
int n;
ll bfs(int v){
ll tmp=0;
fill(mark,mark+maxn,0);
fill(dist,dist+maxn,inf);
dist[v]=1;
tmp++;
mark[v]=1;
q.push(v);
while(!q.empty()){
v = q.front();
q.pop();
for(int u:adj[v]){
if(!mark[u]){
mark[u]=1;
dist[u] = dist[v]+1;
tmp += dist[u];
q.push(u);
}
}
}
for(int i=1;i<=n;i++){
if(!mark[i]){
return inf;
}
}
return tmp;
}
int main(){
cin>>n;
for(int v=1;v<=n;v++){
int x;
cin>>x;
for(int j=0;j<x;j++){
int u;
cin>>u;
adj[u].push_back(v);
}
}
for(int i=1;i<=n;i++){
ans = min(ans,bfs(i));
}
cout<<ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |