This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
* In the name of God
*
* Author: Farbod Doost
* Last Modified: Sun, 19 Feb 2023 (12:52:53)
*
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
const long long inf = 1e18;
int n;
vector <int> adj[N];
long long c[N], ans = inf;
bool vis[N];
queue <int> q;
vector <int> g[N];
void dfs(int v)
{
for (auto u: g[v])
dfs(u), c[v] += c[u];
return;
}
void f(int r)
{
for (int i = 0; i < n; i++)
vis[i] = 0, c[i] = 1, g[i].clear();
q.push(r), vis[r] = 1;
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto u: adj[v])
if (!vis[u])
q.push(u), vis[u] = 1, g[v].push_back(u);
}
dfs(r);
long long sum = 0;
for (int i = 0; i < n; i++) {
if (!vis[i])
return;
sum += c[i];
}
ans = min(ans, sum);
return;
}
signed main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
int k, x;
cin >> k;
while (k--)
cin >> x, x--, adj[x].push_back(i);
}
for (int i = 0; i < n; i++)
f(i);
cout << ans;
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... |