제출 #679790

#제출 시각아이디문제언어결과실행 시간메모리
679790vjudge1Bosses (BOI16_bosses)C++98
67 / 100
1506 ms1008 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define pb push_back

#define fi first

#define se second

#define pii pair<int, int>

#define pll pair<ll, ll>

const int mod=1e9+7, maxn=5e3+5;

ll n, k, x, dist[maxn], a[maxn], cost[maxn], tot, now, ans=LLONG_MAX, cnt;

vector<ll> child[maxn], anak[maxn];

queue<ll> q;



void solve(int now) {

    cost[now]=1;

    for(auto nxt:anak[now]) {

        solve(nxt);

        cost[now]+=cost[nxt];

    }

}



int main() {

    ios_base::sync_with_stdio(false); cin.tie(NULL);

    cin >> n;

    for(int i=1; i<=n; i++) {

        cin >> k;

        while(k--) {

            cin >> x;

            child[x].pb(i);

        }

    }

    for(int i=1; i<=n; i++) {

        for(int j=1; j<=n; j++) anak[j].clear(), a[j]=j, cost[j]=0, dist[j]=-1;

        tot=0, cnt=0;



        dist[i]=0;

        q.push(i);

        while(!q.empty()) {

            now=q.front();

            cnt++;

            q.pop();

            for(auto nxt:child[now]) {

                if(dist[nxt]==-1) {

                    dist[nxt]=dist[now]+1;

                    q.push(nxt);

                    anak[now].pb(nxt);

                }

            }

        }

        if(cnt==n) {

            solve(i);

            for(int j=1; j<=n; j++) {

                tot+=cost[j];

            }

            ans=min(ans, tot);

        }

    }

    cout << ans << '\n';

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...