Submission #606932

#TimeUsernameProblemLanguageResultExecution timeMemory
606932jjianglyBosses (BOI16_bosses)C++14
67 / 100
1568 ms980 KiB
#include <bits/stdc++.h> using namespace std; #define all(x) x.begin(), x.end() #define siz(x) int(x.size()) #define ll long long #define ar array #define vt vector #define inf INT_MAX #define lnf LLONG_MAX const int nxm = int(5e3) + 7; int n, k, u; vt<vt<int>> e(nxm); int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; ++i) { cin >> k; for (int j = 0; j < k; ++j) { cin >> u; --u; e[u].push_back(i); } } int ans = inf; for (int i = 0; i < n; ++i) { vt<vt<int>> e2(nxm); int cnt = 0; function<void()> bfs = [&]() { vt<bool> vis(n, false); queue<ar<int, 2>> que; que.push({i, -1}); while (que.size()) { u = que.front()[0]; int p = que.front()[1]; que.pop(); if (vis[u]) { continue; } ++cnt; if (p != -1) { e2[p].push_back(u); } vis[u] = true; for (int v : e[u]) { if (!vis[v]) { que.push({v, u}); } } } }; bfs(); if (cnt != n) { continue; } vt<int> d(n, 0); function<void(int)> dfs = [&](int root) { int sum = 0; for (int v : e2[root]) { dfs(v); sum += d[v]; } d[root] = sum + 1; }; dfs(i); int sum = 0; for (int j = 0; j < n; ++j) { assert(d[j] > 0); sum += d[j]; } 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...