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;
vector<int>adj[5005];
vector<long long>s(5005);
vector<bool>vis(5005);
void dfs(int n){
vis[n]=1;
s[n]=1;
vector<int>v;
for(auto i:adj[n]){
if(!vis[i]){
vis[i]=1;
v.push_back(i);
}
}
for(auto i:v){
dfs(i);
s[n]+=s[i];
}
}
int main(){
int n;cin>>n;
for(int i=1;i<=n;i++){
int k;cin>>k;
for(int j=0;j<k;j++){
int x;cin>>x;
adj[x].push_back(i);
}
}
long long ans=4e18;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
vis[j]=0;
s[j]=0;
}
dfs(i);
long long sum=0;
for(int j=1;j<=n;j++){
sum+=s[j];
if(!vis[j]){
sum=1e18;
}
}
ans=min(ans,sum);
}
cout<<ans<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |