Submission #540184

#TimeUsernameProblemLanguageResultExecution timeMemory
540184status_codingBosses (BOI16_bosses)C++14
100 / 100
596 ms716 KiB
#include <bits/stdc++.h> using namespace std; long long n,ans; long long h[5005]; vector<int> v[5005]; long long solve(int p) { for(int i=1;i<=n;i++) h[i]=-1; queue<pair<int, int>> q; q.push({p, 1}); while(!q.empty()) { int p = q.front().first; int len = q.front().second; q.pop(); if(h[p] != -1) continue; h[p] = len; for(int it : v[p]) if(h[it] == -1) q.push({it, len+1}); } long long ans=0; for(int i=1;i<=n;i++) { if(h[i] == -1) return 1e18; ans += h[i]; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin>>n; for(int i=1;i<=n;i++) { int k; cin>>k; while(k) { k--; int x; cin>>x; v[x].push_back(i); } } long long ans=1e18; for(int i=1;i<=n;i++) ans=min(ans, solve(i)); cout<<ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...