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 <cmath>
#include <cstdio>
#include <iostream>
#include <utility>
#include <algorithm>
#include <vector>
#include <set>
#include <queue>
#include <map>
using namespace std;
void dfs(vector<vector<int> >&g,vector<int>&s,int n){
s[n]=1;
for(int i:g[n]){
if(s[i]==0){
dfs(g,s,i);
s[n]+=s[i];
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int n,k,a;
cin>>n;
vector<vector<int> >em(n);
for(int i=0;i<n;i++){
cin>>k;
while(k--){
cin>>a;
a--;
em[a].push_back(i);
}
}
vector<pair<int,int> >si(n);
for(int i=0;i<n;i++){
si[i]=make_pair(em[i].size(),i);
}
sort(si.rbegin(),si.rend());
vector<vector<int> >sol(n);
vector<bool>us(n,false);
queue<int>cola;
us[si[0].second]=true;
cola.push(si[0].second);
while(!cola.empty()){
int ac=cola.front();
cola.pop();
for(int i=0;i<em[ac].size();i++){
if(!us[em[ac][i]]){
us[em[ac][i]]=true;
cola.push(em[ac][i]);
sol[ac].push_back(em[ac][i]);
}
}
}
vector<int>sa(n,0);
dfs(sol,sa,si[0].second);
int ans=0;
for(int i=0;i<n;i++){
ans+=sa[i];
}
cout<<ans<<"\n";
}
Compilation message (stderr)
bosses.cpp: In function 'int main()':
bosses.cpp:49:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(int i=0;i<em[ac].size();i++){
| ~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |