제출 #591095

#제출 시각아이디문제언어결과실행 시간메모리
591095IwanttobreakfreeBosses (BOI16_bosses)C++98
100 / 100
703 ms720 KiB
#include <iostream> #include <vector> #include <queue> using namespace std; long long bfs(int a,vector<vector<int>>& g){ int n=g.size(); vector<int> dist(n,1e9); dist[a]=0; queue<int> q; q.push(a); while(!q.empty()){ int u=q.front(); q.pop(); for(int v:g[u]){ if(dist[v]>dist[u]+1){ dist[v]=dist[u]+1; q.push(v); } } } vector<int> cnt(n); for(int i=0;i<n;i++){ if(dist[i]==1e9)return 1e18; cnt[dist[i]]++; } long long ans=0; for(int i=n-1;i>0;i--){ ans+=cnt[i]; cnt[i-1]+=cnt[i]; } return ans+cnt[0]; } int main(){ int n,m,x; cin>>n; vector<vector<int>> g(n,vector<int>()); for(int i=0;i<n;i++){ cin>>m; while(m--){ cin>>x; g[x-1].push_back(i); } } long long ans=1e18; for(int i=0;i<n;i++)ans=min(ans,bfs(i,g)); cout<<ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...