Submission #977489

#TimeUsernameProblemLanguageResultExecution timeMemory
977489JungPSBosses (BOI16_bosses)C++14
100 / 100
412 ms1028 KiB
#include <iostream> #include<cstring> #include<vector> #include<queue> using namespace std; int vis[5007]; long long cost = 0; vector<int> vec[5007]; int n,cnt=0; void bfs(int src) { queue< pair<int , int> >q ; q.push({src , 1}) ; vis[src] = 1 ; while(!q.empty()) { pair<int , int>p = q.front() ; q.pop() ; cnt++ ; int node = p.first ; cost += (p.second * 1ll) ; for(auto &child : vec[node]) { if(vis[child] == 1) continue ; vis[child] = 1 ; q.push({child , p.second+1}) ; } } return ; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); 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(vis , 0 , sizeof(vis)) ; cnt = 0 , cost = 0; bfs(i) ; if(cnt != n) continue; ans = min(ans , cost) ; } return printf("%lld" , ans) , 0 ; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...