Submission #143879

#TimeUsernameProblemLanguageResultExecution timeMemory
143879VlatkoBosses (BOI16_bosses)C++14
100 / 100
1295 ms1796 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

const int maxn = 5001;

int n;
vector<int> adj[maxn];

ll salary[maxn];
int parent[maxn];
vector<int> atdepth[maxn];

ll solve(int root) {
    ll res = 0;
    int numvisited = 1;
    parent[root] = -2;
    salary[root] = 1;
    atdepth[0].push_back(root);
    for (int d = 0; d < n; ++d) {
        // debug(d);
        for (int u : atdepth[d]) {
            // debug(u);
            for (int v : adj[u]) {
                if (parent[v] == -1) {
                    // debug(v);
                    ++numvisited;
                    parent[v] = u;
                    salary[v] = 1;
                    atdepth[d+1].push_back(v);
                }
            }
        }
    }
    for (int d = n-1; d >= 0; --d) {
        while (!atdepth[d].empty()) {
            int u = atdepth[d].back();
            res += salary[u];
            if (parent[u] >= 1) {
                salary[parent[u]] += salary[u];
            }
            parent[u] = -1;
            atdepth[d].pop_back();
        }
    }
    // debug(root, numvisited, res);
    if (numvisited == n) {
        return res;
    } else {
        return 1e18;
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        int k;
        cin >> k;
        while (k--) {
            int u;
            cin >> u;
            adj[u].push_back(i);
        }
    }
    ll ans = 1e18;
    memset(parent, -1, sizeof(parent));
    for (int i = 1; i <= n; ++i) {
        ans = min(ans, solve(i));
    }
    cout << ans << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...