Submission #977496

#TimeUsernameProblemLanguageResultExecution timeMemory
977496JungPSBosses (BOI16_bosses)C++14
100 / 100
411 ms764 KiB
#include<iostream> #include<vector> #include<queue> #include<cstring> #include<utility> using namespace std; vector<int> vec[5007]; bool visited[5007]; long long cost; int cnt; void bfs(int x){ queue<pair<int,int>> q; q.push({x,1}); visited[x]=true; while(!q.empty()){ int node=q.front().first; int cs=q.front().second; ++cnt; q.pop(); cost+=cs; for(auto i:vec[node]){ if(visited[i]) continue; visited[i]=true; q.push({i,cs+1}); } } } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; for(int i=1;i<=n;++i){ int m; cin >> m; while(m--){ int x; cin >> x; vec[x].push_back(i); } } long long ans=1e18; for(int i=1;i<=n;++i){ memset(visited,false,sizeof(visited)); cost=0; cnt=0; bfs(i); if(cnt!=n) continue; ans=min(ans,cost); } cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...