# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
231516 | Haunted_Cpp | Bosses (BOI16_bosses) | C++17 | 1426 ms | 888 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 5;
vector< vector<int> > g (N);
int p [N];
bitset<N> vis;
int subtree [N];
int dfs (int node) {
int sum = 0;
subtree[node] = 1;
int res = 0;
for (auto to : g[node]) {
if (p[to] == node) {
res += dfs (to);
sum += subtree[to];
}
}
subtree[node] += sum;
return res + subtree[node];
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int k;
cin >> k;
for (int j = 0; j < k; j++) {
int st;
cin >> st;
--st;
g[st].emplace_back(i);
}
}
auto bfs = [&] (int source) {
vis.reset ();
vis[source] = 1;
p[source] = -1;
queue<int> q;
q.push(source);
while (!q.empty()) {
int node = q.front();
q.pop();
for (auto to : g[node]) {
if (!vis[to]) {
vis[to] = 1;
p[to] = node;
q.push(to);
}
}
}
if (vis.count() != n) return (int) 1e9;
vis.reset ();
return dfs (source);
};
int mn = 1e9;
for (int i = 0; i < n; i++) {
mn = min (mn, bfs (i));
}
cout << mn << '\n';
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |