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 pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define INF 10000000000//0000000
#define modulo 1000000007
#define mod 998244353
#define int long long int
using namespace std;
vector<int> adj[5005];
vector<int> val(5005, INF);
void bfs(int s){
val[s] = 1;
queue<int> Q;
Q.push(s);
while(!Q.empty()){
int x = Q.front();
Q.pop();
for(auto y : adj[x]){
if(val[y] > val[x] + 1){
Q.push(y);
val[y] = val[x] + 1;
}
}
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for(int i = 1; i <= n; i++){
int k;
cin >> k;
for(int j = 0; j < k; j++){
int x;
cin >> x;
adj[x].pb(i);
}
}
int ans = INF;
for(int i = 1; i <= n; i++){
bfs(i);
int cnt = 0;
for(int j = 1; j <= n; j++){
cnt += val[j];
}
ans = min(ans, cnt);
val = vector<int>(5005, INF);
}
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... |