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 <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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |