Submission #1196134

#TimeUsernameProblemLanguageResultExecution timeMemory
1196134ziewaczBosses (BOI16_bosses)C++20
100 / 100
379 ms988 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define imie(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
const int mod=1e9+7;
const int N=5005;
const int INF=2e9;
vector<int> graf[N];
int dist[N];
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

int bfs(int v) {
    for(int i = 0; i < N; i++) {
        dist[i] = INF;
    }
    queue<int> Q;
    dist[v] = 1;
    Q.push(v);
    int ans = 0;
    while(!Q.empty()) {
        int w = Q.front();
        Q.pop();
        ans += dist[w];
        for(auto &u : graf[w]) {
            if(dist[u] > dist[w] + 1) {
                dist[u] = dist[w] + 1;
                Q.push(u);
            }
        }
    }
    return ans;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        while(x--) {
            int v;
            cin >> v;
            graf[v].push_back(i);
        }
    }
    int ans = 2e9;
    for(int i = 1; i <= n; i++) {
        int wyn = bfs(i);
        bool moge = true;
        for(int j = 1; j <= n; j++) {
            if(dist[j] == INF) {
                moge = false;
            }
        }
        if(moge) ans = min(ans, wyn);
    }
    cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...