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 = 5001;
vector<int> adj[N];
int sz[N], ans = 0, opt = 1e9, cnt = 0;
bool vis[N];
void dfs(int u) {
vis[u] = true;
sz[u] = 1;
cnt++;
for (auto& v : adj[u]) {
if (!vis[v]) {
dfs(v);
sz[u] += sz[v];
}
}
ans += sz[u];
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
for (int i = 1, j, x; i <= n; i++) {
cin >> j;
while (j--) {
cin >> x;
adj[x].push_back(i);
}
}
for (int rt = 1; rt <= n; rt++) {
ans = cnt = 0;
for (int j = 1; j <= n; j++) vis[j] = 0, sz[j] = 0;
dfs(rt);
if (cnt == n) opt = min(opt, ans);
}
cout << opt << '\n';
}
// 1
// 2 3
// 456 78
// 8
// 4 3
// 1 1 1 1 1
// find sigma(sz[u])
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |