# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
717584 | vjudge1 | Bosses (BOI16_bosses) | C++17 | 1 ms | 376 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> adj[3000];
int dp[3000];
int indegree[3000];
int f(int u, int p){
if(dp[u] != -1) return dp[u];
int ans = 1;
for(int v: adj[u]){
if(v != p){
ans += f(v, u);
}
}
return dp[u] = ans;
}
int32_t main(){
cin>>n;
set<int> erb[n];
for(int i = 0; i<n; ++i){
int K; cin>>K;
while(K--){
int kong; cin>>kong;
erb[--kong].insert(i);
}
}
//loop -> find max child
int u;
do{
int maxi = 0;
u = -1;
for(int i = 0; i<n; ++i){
int sz = erb[i].size();
if(sz > maxi){
maxi = sz;
u = i;
}
}
if(u == -1) break;
for(int v: erb[u]){
adj[u].push_back(v);
indegree[v]++;
}
//erase
for(int i = 0; i<n; ++i){
set<int>::iterator it;
it = erb[i].find(u);
if(it != erb[i].end()){
erb[i].erase(it);
}
for(int v: adj[u]){
it = erb[i].find(v);
if(it != erb[i].end()){
erb[i].erase(it);
}
}
}
}while(u != -1);
memset(dp, -1, sizeof(dp));
int root;
for(int i = 0; i<n; ++i){
if(!indegree[i]){
root = i;
break;
}
}
f(root, -1);
int ans = 0;
for(int i = 0; i<n; ++i){
ans += dp[i];
}
cout<<ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |