#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |