Submission #485983

#TimeUsernameProblemLanguageResultExecution timeMemory
485983chungdinhBosses (BOI16_bosses)C++17
100 / 100
971 ms708 KiB
#include <iostream> #include <utility> #include <cstring> #include <vector> #include <queue> #include <stack> using namespace std; #define ii pair<int, int> #define ll long long #define MP(x, y) make_pair(x, y) const int N = 5e3 + 5; const ll INF = 1e18; int n; vector<int> g[N]; bool vsd[N]; int par[N]; ll dp[N]; vector<int> lst; ll bfs(int u) { ll res = 0; queue<int> q; q.push(u); memset(vsd, false, sizeof vsd); vsd[u] = true; for (int i = 1; i <= n; i++) dp[i] = 1; par[u] = -1; lst.clear(); queue<int> q1; while (q.size()) { int u = q.front(); q.pop(); for (int v : g[u]) { if (!vsd[v]) { vsd[v] = true; q.push(v); par[v] = u; } } lst.push_back(u); } for (int i = lst.size(); i >= 0; i--) { int u = lst[i]; if (par[u] > 0) dp[par[u]] += dp[u]; res += dp[u]; } for (int i = 1; i <= n; i++) if (!vsd[i]) return INF; return res; } int main() { #ifdef CHUNGDINH freopen("main.inp", "r", stdin); #endif // CHUNGDINH cin >> n; for (int i = 1; i <= n; i++) { int k; cin >> k; while (k--) { int u; cin >> u; g[u].push_back(i); } } ll res = INF; for (int i = 1; i <= n; i++) res = min(res, bfs(i)); cout << res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...