# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
877484 | Beerus13 | Bosses (BOI16_bosses) | C++14 | 0 ms | 0 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;
#define ll long long
const int N = 5e3 + 5;
int n;
vector<int> g[N];
ll dp[N];
ll bfs(int u) {
queue<int> q;
memset(dp, 0x3f, sizeof(dp));
dp[u] = 1;
q.push(u);
while(q.size()) {
int s = q.front(); q.pop();
for(int t : g[s]) if(dp[t] > dp[s] + 1) {
dp[t] = dp[s] + 1;
q.push(t);
}
}
ll res = 0;
for(int i = 1; i <= n; ++i) res += dp[i];
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i) {
int k, x; cin >> k;
for(int j = 0 j < k; ++j) {
cin >> x;
g[x].push_back(i);
}
}
ll ans = 1e18;
for(int i = 1; i <= n; ++i) ans = min(ans, bfs(i));
cout << ans << '\n';
return 0;
}