This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
بسم الله الرحمن الرحيم
Author:
(:Muhammad Aneeq:)
*/
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int const N=5e3+10;
vector<int>nei[N]={};
bool vis[N]={};
vector<int>gr[N]={};
int val[N]={};
long long cost=0;
void make_gr(int n)
{
queue<int>Q;
Q.push(n);
while (Q.size())
{
int s=Q.front();
Q.pop();
for (auto i:gr[s])
if (!vis[i])
{
vis[i]=1;
nei[s].push_back(i);
Q.push(i);
}
}
}
void dfs(int n)
{
int val1=0;
for (auto i:nei[n])
{
dfs(i);
val1+=val[i];
}
val[n]=val1+1;
cost+=val[n];
}
inline void solve()
{
int n;
cin>>n;
for (int i=1;i<=n;i++)
{
int k;
cin>>k;
while (k--)
{
int x;
cin>>x;
gr[x].push_back(i);
}
}
long long ans=1e15+10;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
vis[j]=0,nei[j]={};
val[i]=0;
}
vis[i]=1;
make_gr(i);
bool w=0;
for (int i=1;i<=n;i++)
{
if (!vis[i])
w=1;
}
if (w)
continue;
cost=0;
dfs(i);
ans=min(ans,cost);
}
cout<<ans<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |