Submission #1308797

#TimeUsernameProblemLanguageResultExecution timeMemory
1308797michud07Bosses (BOI16_bosses)C++20
100 / 100
363 ms728 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 5004;
vector<int> accepting[N];
int depth[N];
queue<int> q;

int check(int v, int n){
    for(int i = 1 ; i <= n ; i++){
        depth[i] = -1;
    }
    int ans = 1, cleared = 1;
    depth[v] = 0;
    q.push(v);
    while(!q.empty()){
        v = q.front();
        q.pop();
        for(auto u : accepting[v]){
            if(depth[u] == -1){
                cleared++;
                depth[u] = depth[v] + 1;
                ans += depth[u] + 1;
                q.push(u);
            }
        }
    }
    return cleared == n ? ans : 1e9;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k, v;
    cin >> n;
    for(int i = 1 ; i <= n ; i++){
        cin >> k;
        while(k--){
            cin >> v;
            accepting[v].push_back(i);
        }
    }
    int ans = 1e9;
    for(int i = 1 ; i <= n ; i++){
        ans = min(ans, check(i, n));
    }
    cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...