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;
vector<vector<int>> adg;
vector<vector<int>> adgtree;
void dfs(int cur, long long salary[]){
if(adgtree[cur].empty()){
salary[cur]= 1;
}
else{
long long cnt = 0;
for(int next : adgtree[cur]){
dfs(next, salary);
cnt+=salary[next];
}
salary[cur]=cnt+1;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
adg.resize(n);
for(int i=0; i<n; i++){
int a;
cin >> a;
for(int j=0; j<a; j++){
int b;
cin >> b;
adg[b-1].push_back(i);
}
}
long long ans = pow(10, 17);
for(int root = 0; root<n; root++){
bool vsted[n] = {false};
adgtree.clear();
adgtree.resize(n);
queue<int> que;
que.push(root);
vsted[root]=true;
while(!que.empty()){
int cur = que.front();
que.pop();
for(int u : adg[cur]){
if(!vsted[u]){
vsted[u]=true;
adgtree[cur].push_back(u);
que.push(u);
}
}
}
long long salary[n];
dfs(root, salary);
long long curans = 0;
for(int i=0; i<n; i++){
curans+=salary[i];
}
ans = min(ans, curans);
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |