제출 #350637

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

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

int n,ans,sum,sz;
bool check;
vector<int>G[maxn];
vector<int>H[maxn];
bool marked[maxn];
int val[maxn];

void bfs(int v){
    marked[v] = true;
    queue<int>q;
    q.push(v);
    while(!q.empty()){
        int p = q.front();
        q.pop();
        sz++;
        H[p].clear();
        for(int b : G[p]){
            if(marked[b]) continue;
            marked[b] = true;
            q.push(b);
            H[p].push_back(b);
        }
    }
}

void dfs(int v){
    int s = 0;
    sz++;
    for(int b : H[v]){
        dfs(b);
        s += val[b];
    }
    val[v] = s + 1;
    sum += val[v];
    if(ans <= sum){
        check = false;
        return;
    }
}

int32_t main(){
    ios:: sync_with_stdio(0),cin.tie(0),cout.tie(0);
    ////////////////////////////////////////////////
    ans = inf;
    sum = 0;
    cin >> n;
    for(int i = 0;i<n;i++){
        int x; cin >> x;
        for(int j = 0;j<x;j++){
            int y; cin >> y;
            G[y].push_back(i+1);
        }
    }
    for(int i = 0;i<n;i++){
        fill(marked,marked+n+1,false);
        sz = 0;
        bfs(i+1);
        if(sz != n){
            continue;
        }
        check = true;
        sum = 0;
        sz = 0;
        dfs(i+1);
        if(check == true && sz == n){
            ans = min(ans,sum);
        }
    }
    cout << ans << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...