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>
#define int long long
#define INF 1000000000000000
#define lchild(i) (i*2 + 1)
#define rchild(i) (i*2 + 2)
#define mid(l, u) ((l+u)/2)
#define x(p) p.first
#define y(p) p.second
#define MOD 998244353
using namespace std;
int cost = 0;
int calcost(int i, vector<int> adj[]){
int sum = 0;
for(int j: adj[i]){
sum+=calcost(j, adj);
}
//cout<<"node "<<i<<" value "<<(sum+1)<<"\n";
cost += (sum + 1);
return (sum + 1);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
vector<int> children[n+1];
for(int i =0 ;i<n;i++){
int k;
cin>>k;
while(k--){
int par;
cin>>par;
children[par].push_back(i+1);
}
}
int ans = INF;
for(int i = 1;i<=n;i++){
vector<int> adj[n+1];
bool chosen[n+1];
for(int j = 0;j<=n;j++) chosen[j] = false;
chosen[i] = true;
queue<int> q;
q.push(i);
int cnt = 1;
while(!q.empty()){
int curr = q.front();
q.pop();
for(int j: children[curr]){
if(chosen[j]) continue;
cnt++;
chosen[j] = true;
q.push(j);
adj[curr].push_back(j);
}
}
if(cnt!=n) continue;
cost = 0;
calcost(i, adj);
ans = min(ans, cost);
}
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... |