제출 #388160

#제출 시각아이디문제언어결과실행 시간메모리
388160SaraBosses (BOI16_bosses)C++17
100 / 100
753 ms612 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define ll long long
#define F first
#define S second
#define pb push_back
#define lc (ind << 1)
#define rc (lc | 1)
#define md ((b + e) >> 1)

const ll N = 5000 + 5;
const ll M = 1e6 + 5;
const ll LOG = 30;
const ll INF = 1e9 + 5;
const ll MOD = 1e9 + 7;

int n;
vector <int> g[N];
ll ans = INF * INF;
ll h[N];
queue <int> q;

inline ll bfs(int r){
    fill(h + 1, h + n + 1, INF);
    h[r] = 0;
    q.push(r);
    while (q.size()){
        int v = q.front();
        q.pop();
        for (int u : g[v]){
            if (h[v] + 1 < h[u]){
                h[u] = h[v] + 1;
                q.push(u);
            }
        }
    }
    ll sum = 0;
    for (int i = 1; i <= n; i ++){
        sum += h[i];
    }
    return sum + n;
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    for (int v = 1; v <= n; v ++){
        int m;
        cin >> m;
        for (int i = 0; i < m; i ++){
            int u;
            cin >> u;
            g[u].pb(v);
        }
    }
    for (int v = 1; v <= n; v ++){
        ans = min(ans, bfs(v));
    }
    cout << ans << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...