# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1122986 | I_FloPPed21 | Bosses (BOI16_bosses) | C++20 | 0 ms | 0 KiB |
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int N=5e3+2;
int n;
//observatie generala costul e size-ul subtreeului
vector<int>prioritate[N];
vector<int>adj[N];
void citeste()
{
cin>>n;
for(int i=1;i<=n;i++)
{
int size;
cin>>size;
for(int indice=1;indice<=size;indice++)
{
int sef;
cin>>sef;
prioritate[sef].push_back(i);
}
}
}
long long dp[N];
int global=0;
long long ans=1e12;
void dfs(int nod,int p)
{
dp[nod]=1;
for(auto u:adj[nod])
{
if(u!=p)
{
dfs(u,nod);
dp[nod]+=dp[u];
}
}
global+=dp[nod];
}
void parcurge(int sef)
{
queue<int>q;
q.push(sef);
vector<bool>viz(n+1,0);
viz[sef]=true;
for(int i=1;i<=n;i++)
{
adj[i].clear();
}
while(!q.empty())
{
int nod=q.front();
for(auto u:prioritate[nod])
{
if(viz[u]==false)
{
adj[u].push_back(nod);
adj[nod].push_back(u);
viz[u]=true;
q.push(u);
}
}
q.pop();
}
global=0;
dfs(sef,0);
ans=min(ans,global);
}
void rezolva()
{
for(int i=1;i<=n;i++)
{
parcurge(i);
}
cout<<ans<<'\n';
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
citeste();
rezolva();
return 0;
}