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 ;
typedef long long ll ;
typedef unsigned long long ull ;
#define fi first
#define se second
// mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int MAXN = 5007 ;
int n ;
vector < int > v[ MAXN ] ;
int dist[ MAXN ] ;
queue < int > q ;
int bfs ( int ori ) {
for ( int i = 1 ; i <= n ; ++ i ) {
dist[ i ] = MAXN ;
}
int ret = 0 ;
dist[ ori ] = 1 ;
q.push ( ori ) ;
while ( q.empty ( ) == false ) {
int x = q.front ( ) ; q.pop ( ) ;
ret += dist[ x ] ;
for ( auto y : v[ x ] ) {
if ( dist[ y ] == MAXN ) {
dist[ y ] = dist[ x ] + 1 ;
q.push ( y ) ;
}
}
}
for ( int i = 1 ; i <= n ; ++ i ) {
if ( dist[ i ] == MAXN ) { return MAXN * MAXN ; }
}
return ret ;
}
void solve ( ) {
cin >> n ;
for ( int i = 1 ; i <= n ; ++ i ) {
int sz ; cin >> sz ;
for ( int j = 0 , x ; j < sz ; ++ j ) {
cin >> x ;
v[ x ].push_back ( i ) ;
}
}
int ans = MAXN * MAXN ;
for ( int i = 1 ; i <= n ; ++ i ) {
ans = min ( ans , bfs ( i ) ) ;
}
cout << ans << "\n" ;
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ; // cin >> t ;
while ( t -- ) { solve ( ) ; }
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... |