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;
using ll = long long;
const int maxn = 5005;
int n, k, x, seen=0;
ll ans = 1e18, dp[maxn+5];
vector<int> graph[maxn+5], tree[maxn+5], vis(maxn+5);
queue<int> q;
ll dfs(int u) {
dp[u] = 1;
for(int &v : tree[u]) dp[u] += dfs(v);
return dp[u];
}
int32_t main() {
cin >> n;
for(int i=1; i<=n; i++) {
cin >> k;
for(int j=0; j<k; j++) {
cin >> x;
graph[x].push_back(i);
}
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) vis[j] = dp[j] = 0, tree[j].clear();
q.push(i); vis[i] = 1; seen = 0;
while(!q.empty()) {
int u = q.front();
q.pop();
seen++;
for(int &v : graph[u]) {
if(vis[v]) continue;
vis[v] = 1;
tree[u].push_back(v);
q.push(v);
}
}
if(seen < n) continue;
dfs(i);
ll res = 0;
for(int j=1; j<=n; j++) res += dp[j];
ans = min(ans, res);
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |