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 <iostream>
#include <utility>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define ii pair<int, int>
#define ll long long
#define MP(x, y) make_pair(x, y)
const int N = 5e3 + 5;
const ll INF = 1e18;
int n;
vector<int> g[N];
bool vsd[N];
int par[N];
ll dp[N];
vector<int> lst;
ll bfs(int u) {
ll res = 0;
queue<int> q; q.push(u);
memset(vsd, false, sizeof vsd); vsd[u] = true;
for (int i = 1; i <= n; i++) dp[i] = 1;
par[u] = -1;
lst.clear();
queue<int> q1;
while (q.size()) {
int u = q.front(); q.pop();
for (int v : g[u]) {
if (!vsd[v]) {
vsd[v] = true;
q.push(v);
par[v] = u;
}
}
lst.push_back(u);
}
for (int i = lst.size(); i >= 0; i--) {
int u = lst[i];
if (par[u] > 0) dp[par[u]] += dp[u];
res += dp[u];
}
return res;
}
int main() {
#ifdef CHUNGDINH
freopen("main.inp", "r", stdin);
#endif // CHUNGDINH
cin >> n;
for (int i = 1; i <= n; i++) {
int k; cin >> k;
while (k--) {
int u; cin >> u;
g[u].push_back(i);
}
}
ll res = INF;
for (int i = 1; i <= n; i++) res = min(res, bfs(i));
cout << res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |