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 N=5e3+10;
set<int> tp[N];
vector<int> ma[N];
int h[N],val[N];
void compute_answer(int&v)
{
val[v]=0;
for(int&ap:ma[v])
{
compute_answer(ap);
val[v]+=val[ap];
}
val[v]++;
}
int main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
int k;
cin>>k;
for(int j=1;j<=k;j++)
{
int p;
cin>>p;
tp[p].insert(i);
}
}
int ans=1e9;
for(int i=1;i<=n;i++)
{
for(int ap=1;ap<=n;ap++)
{
ma[ap].clear();
h[ap]=1e9;
}
{
//Time = O()
queue<int> q;
q.push(i);
h[i]=0;
while(q.size())
{
int f=q.front();
q.pop();
for(auto ap:tp[f])
{
if((h[ap]>(h[f]+1)))
{
h[ap]=h[f]+1;
ma[f].push_back(ap);
q.push(ap);
}
}
}
}
compute_answer(i);
int sum=0;
for(int i=1;i<=n;i++)
{
if(h[i]==1e9)
{
sum=1e9;
break;
}
sum+=val[i];
}
ans=min(ans,sum);
}
cout<<ans<<'\n';
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... |