#include <bits/stdc++.h>
using namespace std;
const int M = 5000;
vector<int> nei[M],chi[M];
bool vis[M];
int subt[M],cst;
void dfs(int u)
{
subt[u]=1;
for (int i:chi[u])
{
dfs(i);
subt[u]+=subt[i];
}
cst+=subt[u];
}
void bfs(int v)
{
vis[v]=true;
queue<int> q;
q.push(v);
while (!q.empty())
{
int x=q.front();
q.pop();
for (int i:nei[x])
if (!vis[i])
{
chi[x].push_back(i);
q.push(i);
vis[i]=true;
}
}
dfs(v);
}
int main()
{
int n;
cin>>n;
for (int i=0;i<n;i++)
{
int k;
cin>>k;
for (int j=0;j<k;j++)
{
int x;
cin>>x;
x--;
nei[x].push_back(i);
}
}
int ans=n*(n+1)/2;
for (int i=0;i<n;i++)
{
bfs(i);
ans=min(ans,cst);
cst=0;
for (int j=0;j<n;j++)
{
vis[j]=false;
chi[j].clear();
}
}
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |