# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
202354 | CaroLinda | Bosses (BOI16_bosses) | C++14 | 1122 ms | 888 KiB |
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>
const int MAXN = 5e3 + 10 ;
const int inf = 2e9+10 ;
using namespace std ;
int N , K , my_boss ;
long long ans = inf , cur_ans ;
int deg[MAXN] , par[MAXN] ;
long long val[MAXN] ;
vector<int> adj[MAXN];
bool vis[MAXN] ;
inline void bfs(int S)
{
memset(vis, false, sizeof vis ) ;
for(int i = 1 ; i <= N ; i++ ) val[i] = 0 ;
queue<int> fila ;
vector<int> ordem ;
fila.push(S) ;
par[S] = -1 ;
vis[S] = true ;
ordem.push_back(S) ;
while(!fila.empty() )
{
int curr = fila.front() ;
fila.pop() ;
for(int viz : adj[curr] )
{
if( vis[viz] ) continue ;
vis[viz] = true ;
fila.push(viz) ;
par[viz] = curr ;
ordem.push_back( viz ) ;
}
}
for(int i = 1 ; i <= N ; i++ )
if(!vis[i]) return ;
reverse(ordem.begin() ,ordem.end() ) ;
for(int i : ordem )
{
val[i] += 1LL ;
if( par[i] != -1 )
val[ par[i] ] += val[i] ;
}
cur_ans = 0LL ;
for(int i = 1 ; i <= N ; i++ )
cur_ans += val[i] ;
ans = min(ans,cur_ans ) ;
}
int main()
{
scanf("%d", &N ) ;
for(int i = 1 ; i <= N ; i++ )
{
scanf("%d", &K ) ;
for(int j = 0 ; j < K ; j++ )
{
scanf("%d", &my_boss ) ;
adj[ my_boss ].push_back( i ) ;
}
}
for(int i = 1 ; i <= N ; i++ )
bfs(i) ;
printf("%lld\n" , ans ) ;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |