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;
void dfs(int cur, vector<int> adgtree[], long long salary[]){
    if(adgtree[cur].empty()){
		salary[cur]=1;
    }
    else{
        long long cnt = 0;
        for(int next : adgtree[cur]){
            dfs(next, adgtree, salary);
            cnt+=salary[next];
        }
        salary[cur]=cnt+1;
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<int> adg[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 = INT64_MAX;
    for(int root = 0; root<n; root++){
        bool vsted[n] = {false};
        vector<int> adgtree[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, adgtree, 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... |