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 = 5005;
vector<int> adj[N], adj_tmp[N];
int sum = 0;
int dfs(int i) {
int ret = 1;
for (auto &j : adj_tmp[i]) {
ret += dfs(j);
}
sum += ret;
return ret;
}
int main() {
int n; cin >> n;
for (int i = 1; i <= n; i++) {
int k; cin >> k;
for (int j = 1; j <= k; j++) {
int x; cin >> x;
adj[x].push_back(i);
}
}
int ans = INT_MAX;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
adj_tmp[j].clear();
}
queue<pair<int,int>> q; q.push({i, 0});
vector<int> vst(n + 1);
while (q.size()) {
auto i = q.front(); q.pop();
if (vst[i.first]) continue;
vst[i.first] = 1;
adj_tmp[i.second].push_back(i.first);
for (auto &j : adj[i.first]) {
q.push({j, i.first});
}
}
bool flag = false;
for (int j = 1; j <= n; j++) {
if (!vst[j]) {
flag = true;
break;
}
}
if (!flag) {
sum = 0;
dfs(i);
ans = min(ans, sum);
}
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |