#include <bits/stdc++.h>
#define N 5010
#define inf 2000000000
using namespace std;
int n, used[N], dp[N];
vector<int> filho[N], grafo[N];
int dfs(int x, int p)
{
dp[x] = 1;
for(auto v: grafo[x])
{
if(v == p) continue;
dp[x] += dfs(v, x);
}
return dp[x];
}
int add(int root)
{
queue<int> bfs;
for(int i = 0; i < N; i++) grafo[i].clear(), used[i] = 0, dp[i] = 0;
bfs.push(root), used[root] = 1;
while(!bfs.empty())
{
int x = bfs.front();
bfs.pop();
for(auto v: filho[x])
{
if(!used[v])
{
used[v] = 1;
grafo[x].push_back(v);
grafo[v].push_back(x);
bfs.push(v);
}
}
}
dfs(root, root);
int cnt = 0;
for(int i = 1; i <= n; i++) cnt += dp[i];
for(int i = 1; i <= n; i++) if(dp[i] == 0) cnt = inf;
return cnt;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>n;
for(int p = 1, k, x; p <= n; p++)
{
cin>>k;
for(int it = 1; it <= k; it ++)
{
cin>>x;
filho[x].push_back(p);
}
}
int ans = 2000000000;
for(int root = 1; root <= n; root ++)
{
ans = min(ans, add(root));
//cout << root << " " << add(root)<<"\n";
}
cout<<ans<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
608 KB |
Output is correct |
3 |
Correct |
2 ms |
660 KB |
Output is correct |
4 |
Correct |
2 ms |
732 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
608 KB |
Output is correct |
3 |
Correct |
2 ms |
660 KB |
Output is correct |
4 |
Correct |
2 ms |
732 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
3 ms |
1052 KB |
Output is correct |
11 |
Correct |
3 ms |
1052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
608 KB |
Output is correct |
3 |
Correct |
2 ms |
660 KB |
Output is correct |
4 |
Correct |
2 ms |
732 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
3 ms |
1052 KB |
Output is correct |
11 |
Correct |
3 ms |
1052 KB |
Output is correct |
12 |
Correct |
9 ms |
1112 KB |
Output is correct |
13 |
Correct |
7 ms |
1192 KB |
Output is correct |
14 |
Correct |
508 ms |
1620 KB |
Output is correct |
15 |
Correct |
103 ms |
1620 KB |
Output is correct |
16 |
Correct |
1477 ms |
1632 KB |
Output is correct |
17 |
Execution timed out |
1558 ms |
1632 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |