Submission #509492

#TimeUsernameProblemLanguageResultExecution timeMemory
509492ac2huBosses (BOI16_bosses)C++14
100 / 100
1269 ms768 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 5e3 + 10; int n; vector<int> adj[N]; int visited[N]; int par[N]; int sub[N]; int val[N]; signed main(){ iostream::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr); cin >> n; for(int i = 0;i<n;i++){ int c;cin >> c; for(int j = 0;j<c;j++){ int temp;cin >> temp; temp --; adj[temp].push_back(i); } } int ans = 1e18; for(int i = 0;i<n;i++){ // This will the root of the tree for(int i = 0;i<n;i++){ visited[i] =false; par[i] = -1; sub[i] = 0; val[i] = 1; } queue<int> q; queue<int> q2; q.push(i); visited[i] = true; while(!q.empty()){ int x = q.front(); // cout << x << "==> "; q.pop(); for(int e : adj[x]){ if(visited[e]) continue; else{ visited[e] = true; par[e] = x; sub[x]++; q.push(e); // cout << e << " "; } } // cout << "\n"; if(sub[x] == 0) q2.push(x); } bool flag = false; for(int i = 0;i<n;i++){ if(!visited[i]){ // cout << i << "\n"; flag = true; } } // for(int i = 0;i<n;i++) // cout<< par[i] << " "; // cout << "\n"; if(flag)continue; int temp = 0; while(!q2.empty()){ int x = q2.front(); q2.pop(); temp += val[x]; // cout << x << " " << val[x] << "\n"; if(par[x] == -1) continue; sub[par[x]]--; val[par[x]] += val[x]; if(sub[par[x]] == 0) q2.push(par[x]); } ans = min(ans,temp); } cout << ans << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...