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 int long long
#define INF 1e18
#define endl '\n'
using namespace std;
int n , k , ans = 1e18 , dist[200005];
vector < int > adj[200005];
bool vis[200005];
void bfs(int s){
queue < int > q;
vis[s] = true;
q.push(s);
dist[s] = 1;
int ttl = 1;
while(!q.empty()){
int u = q.front(); q.pop();
for(int i : adj[u]){
if(!vis[i]){
vis[i] = true;
q.push(i);
dist[i] = dist[u] + 1;
ttl += dist[i];
}
}
}
bool bs = true;
for(int i = 1 ; i <= n ; i++) if(!vis[i]) bs = false;
if(bs) ans = min(ans , ttl);
}
signed main(){
cin >> n;
for(int i = 1 ; i <= n ; i++){
cin >> k;
for(int j = 1 ; j <= k ; j++){
int tmp; cin >> tmp;
adj[tmp].pb(i);
}
}
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= n ; j++){
vis[j] = false;
dist[j] = 0;
}
bfs(i);
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |