Submission #489596

#TimeUsernameProblemLanguageResultExecution timeMemory
489596SlavicGBosses (BOI16_bosses)C++17
100 / 100
1311 ms948 KiB
#include "bits/stdc++.h" using namespace std; #define ll long long #define forn(i,n) for(int i=0;i<n;i++) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(),v.rend() #define pb push_back #define sz(a) (int)a.size() const int N = 5005; bool vis[N]; vector<int> adj[N]; vector<int> g[N]; int c[N]; int f = 0; void bfs(int u){ forn(i, N)g[i].clear(), vis[i] = false; vis[u] = true; queue<int> q; q.push(u); vis[u] = true; while(!q.empty()){ int i = q.front(); q.pop(); for(int v: adj[i]){ if(!vis[v]){ q.push(v); g[i].pb(v); vis[v] = true; } } } } void dfs(int u, int par){ ll s = 0; for(int v: g[u]){ if(v == par)continue; dfs(v, u); s += c[v]; } c[u] = s + 1; f += c[u]; } void solve() { int n; cin >> n; for(int i = 0;i < n; ++i){ int k; cin >> k; while(k--){ int a; cin >> a; --a; adj[a].pb(i); } } int ans = INT_MAX; for(int i = 0;i < n; ++i){ bfs(i); bool ok = true; for(int j = 0;j < n; ++j){ if(!vis[j])ok = false; } if(!ok)continue; f = 0; dfs(i, -1); ans = min(ans, f); } cout << ans << "\n"; } int32_t main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int t = 1; //cin >> t; while(t--) { solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...