//In the name of God
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MXN = 5e3 + 10;
const ll INF = 1e18;
ll n, sum, ted;
ll dis[MXN];
vector<ll> adj[MXN];
queue<ll> q;
bool vis[MXN];
ll BFS(ll src){
memset(dis, 63, sizeof dis), memset(vis, 0, sizeof vis);
dis[src] = 1, vis[src] = 1;
q.push(src), sum = 0, ted = 0;
while(!q.empty()){
ll u = q.front(); q.pop();
sum += dis[u], ted ++;
for(auto v : adj[u]){
if(!vis[v]){
vis[v] = 1, dis[v] = dis[u] + 1;
q.push(v);
}
}
}
//cout << src << ' ' << sum << ' ' << ted << '\n';
return (ted == n ? sum : INF);
}
int main(){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i ++){
ll k; cin >> k;
for(int j = 1, x; j <= k; j ++) cin >> x, adj[x].push_back(i);
}
ll ans = INF;
for(int i = 1; i <= n; i ++) ans = min(ans, BFS(i));
cout << ans << '\n';
return 0;
}
//! This is NIMA !
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
448 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
448 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
5 ms |
588 KB |
Output is correct |
13 |
Correct |
4 ms |
588 KB |
Output is correct |
14 |
Correct |
141 ms |
592 KB |
Output is correct |
15 |
Correct |
11 ms |
588 KB |
Output is correct |
16 |
Correct |
599 ms |
716 KB |
Output is correct |
17 |
Correct |
719 ms |
684 KB |
Output is correct |
18 |
Correct |
774 ms |
684 KB |
Output is correct |