제출 #700880

#제출 시각아이디문제언어결과실행 시간메모리
700880MakarooniBosses (BOI16_bosses)C++17
100 / 100
1373 ms944 KiB
#include <bits/stdc++.h>
using namespace std;

#define pb(x) push_back(x)
typedef long long ll;

const int N = 5e3 + 10;
const int inf = 1e9;

vector<int> g[N], G[N];
int dis[N], ans[N];

void dfs(int v){
    ans[v] = 1;
    for(int u : G[v]){
        dfs(u);
        ans[v] += ans[u];
    }
}

int main(){
    int n;
    cin >> n;
    int k, v;
    for(int i = 1; i <= n; i++){
        cin >> k;
        for(int j = 1; j <= k; j++){
            cin >> v;
            g[v].pb(i);
        }
    }
    ll Ans = inf;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            dis[j] = inf;
            G[j].clear();
            ans[j] = inf;
        }
        queue<int> q;
        dis[i] = 0;
        q.push(i);
        while(!q.empty()){
            v = q.front();
            q.pop();
            for(int u : g[v]){
                if(dis[u] == inf){
                    dis[u] = dis[v] + 1;
                    G[v].pb(u);
                    q.push(u);
                }
            }
        }
        dfs(i);
        ll sal = 0;
        for(int j = 1; j <= n; j++)
            sal += ans[j];
        Ans = min(Ans, sal);
    }
    cout << Ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...