Submission #695232

#TimeUsernameProblemLanguageResultExecution timeMemory
695232Ahmed_SolymanBosses (BOI16_bosses)C++14
100 / 100
1489 ms884 KiB
#include<bits/stdc++.h> using namespace std; vector<int>adj[5005]; vector<long long>s(5005); vector<bool>vis(5005); vector<int>g[5005]; void dfs1(int n){ queue<int>q; q.push(n); while(q.size()){ int x=q.front(); q.pop(); vis[x]=1; for(auto i:adj[x]){ if(!vis[i]){ g[x].push_back(i); vis[i]=1; q.push(i); } } } } long long dfs2(int n){ if(s[n]){ return s[n]; } long long sum=1; for(auto i:g[n]){ dfs2(i); sum+=s[i]; } return s[n]=sum; } 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; g[j].clear(); } dfs1(i); long long sum=0; bool valid=1; for(int j=1;j<=n;j++){ if(!vis[j]){ valid=0; } } if(valid){ for(int j=1;j<=n;j++){ sum+=dfs2(j); } ans=min(ans,sum); } } if(ans==4e18)while(1)cout<<"g"; cout<<ans<<endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...