Submission #1275911

#TimeUsernameProblemLanguageResultExecution timeMemory
1275911nanaseyuzukiBosses (BOI16_bosses)C++20
100 / 100
377 ms776 KiB
#include <bits/stdc++.h>
// Author: Kazuki_Will_Win_VOI_8703
#define fi first
#define se second
#define pii pair<int, int>
using namespace std;

const int mn = 1e6 + 5, bm = (1 << 20) + 1;
const int inf = 1e9;

int n, k[mn], d[mn];
vector <int> a[mn];

void solve(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> k[i];
        for(int j = 1; j <= k[i]; j++){
            int x; cin >> x;
            a[x].push_back(i);
        }
    }
    int res = inf;
    for(int i = 1; i <= n; i++){
        fill(d, d + n + 1, inf);
        d[i] = 0;
        queue <int> pq; pq.push(i);
        while(pq.size()){
            int u = pq.front();
            pq.pop();
            for(auto v : a[u]){
                if(d[v] > d[u] + 1){
                    d[v] = d[u] + 1;
                    pq.push(v);
                }
            }
        }
        int sum = 0;
        for(int i = 1; i <= n; i++){
            if(d[i] == inf){
                d[i] = sum = inf;
                break;
            }
            sum += d[i];
        }
        res = min(res, sum);
    }
    cout << res + n << '\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // cin >> t;
    while(t--) solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...