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 pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int mxn = 5050;
const ll inf = 1e9;
ll dist[mxn];
queue<int> q;
vector<int> paths[mxn];
int N,M;
ll BFS(int s){
q.push(s);
fill(dist,dist+N+1,inf);
dist[s] = 1;
q.push(s);
while(!q.empty()){
auto now = q.front();
q.pop();
for(auto nxt:paths[now]){
if(dist[nxt]>dist[now]+1){
dist[nxt] = dist[now]+1;
q.push(nxt);
}
}
}
return accumulate(dist+1,dist+N+1,0ll);
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N;
for(int i = 1;i<=N;i++){
int k;
cin>>k;
while(k--){
int tmp;
cin>>tmp;
paths[tmp].push_back(i);
}
}
ll ans = inf*inf;
for(int i = 1;i<=N;i++)ans = min(ans,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... |