#include <bits/stdc++.h>
using namespace std;
int n, ans = 1000000000;
vector<int> adj[5001];
vector<int> adj_tr[5001];
vector<int> dp(5001);
vector<int> dist(5001), prv(5001);
void dfs(int now) {
int s = 0;
for (int u: adj_tr[now]) {
dfs(u);
s += dp[u];
}
dp[now] = s+1;
}
int bfs(int fi) {
for (int i = 1; i <= n; i++) {
adj_tr[i].clear(); dp[i] = 0; prv[i] = 0; dist[i] = 0;
}
queue<int> q;
q.push(fi); dist[fi] = 1;
while (q.size()) {
int now = q.front(); q.pop();
for (int u: adj[now]) {
if (dist[u] == 0) {
dist[u] = dist[now] + 1;
q.push(u); prv[u] = now;
}
}
}
for (int i = 1; i <= n; i++) {
if (i != fi) {
if (prv[i] == 0) return ans;
adj_tr[prv[i]].push_back(i);
}
}
dfs(fi);
int rs = 0;
for (int i = 1; i <= n; i++) {
rs += dp[i];
}
return rs;
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
int k; cin >> k;
while (k--) {
int x; cin >> x;
adj[x].push_back(i);
}
}
for (int i = 1; i <= n; i++) {
ans = min(ans, bfs(i));
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
640 KB |
Output is correct |
2 |
Correct |
4 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
640 KB |
Output is correct |
2 |
Correct |
4 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
640 KB |
Output is correct |
2 |
Correct |
4 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
5 ms |
640 KB |
Output is correct |
12 |
Correct |
12 ms |
768 KB |
Output is correct |
13 |
Correct |
8 ms |
768 KB |
Output is correct |
14 |
Correct |
201 ms |
888 KB |
Output is correct |
15 |
Correct |
49 ms |
768 KB |
Output is correct |
16 |
Correct |
675 ms |
888 KB |
Output is correct |
17 |
Execution timed out |
1590 ms |
896 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |