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 <iostream>
#include <queue>
#include <assert.h>
using namespace std;
typedef long long int ll;
ll N,K;
ll x;
bool B[5005][5005],V[5005];
int P[5005];
queue<int> Q;
vector<int> albero[5005];
vector<int> grafo[5005];
ll ans=1e18,res;
ll S[5005];
int c=-1;
void DFS(int k){
for(int f:albero[k]){
DFS(f);
S[k]+=S[f];
}
S[k]++;
res+=S[k];
}
int main(){
cin>>N;
for(int i=0;i<N;i++){
cin>>K;
for(int j=0;j<K;j++){
cin>>x;
x--;
grafo[x].push_back(i);
}
}
for(int i=0;i<N;i++){
fill(V,V+N,false);
V[i]=true;
Q.push(i);
for(int j=0;j<N;j++)
albero[j].clear();
fill(S,S+N,0);
res=0;
x=1;
while(!Q.empty()){
int nodo=Q.front();
Q.pop();
for(int f:grafo[nodo])
if(!V[f]){
Q.push(f);
V[f]=true;
albero[nodo].push_back(f);
x++;
}
}
if(x!=N)
continue;
DFS(i);
ans=min(ans,res);
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |