Submission #951491

#TimeUsernameProblemLanguageResultExecution timeMemory
951491Muhammad_AneeqBosses (BOI16_bosses)C++17
100 / 100
884 ms1028 KiB
/* بسم الله الرحمن الرحيم Author: (:Muhammad Aneeq:) */ #include <iostream> #include <vector> #include <queue> using namespace std; int const N=5e3+10; vector<int>nei[N]={}; bool vis[N]={}; vector<int>gr[N]={}; int val[N]={}; long long cost=0; void make_gr(int n) { queue<int>Q; Q.push(n); while (Q.size()) { int s=Q.front(); Q.pop(); for (auto i:gr[s]) if (!vis[i]) { vis[i]=1; nei[s].push_back(i); Q.push(i); } } } void dfs(int n) { int val1=0; for (auto i:nei[n]) { dfs(i); val1+=val[i]; } val[n]=val1+1; cost+=val[n]; } inline void solve() { int n; cin>>n; for (int i=1;i<=n;i++) { int k; cin>>k; while (k--) { int x; cin>>x; gr[x].push_back(i); } } long long ans=1e15+10; for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) { vis[j]=0,nei[j]={}; val[i]=0; } vis[i]=1; make_gr(i); bool w=0; for (int i=1;i<=n;i++) { if (!vis[i]) w=1; } if (w) continue; cost=0; dfs(i); ans=min(ans,cost); } cout<<ans<<endl; } int main() { ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...