Submission #493494

#TimeUsernameProblemLanguageResultExecution timeMemory
493494PoPularPlusPlusBosses (BOI16_bosses)C++17
100 / 100
1133 ms840 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long 
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007; 
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}

void solve(){
	int n;
	cin >> n;
	vector<int> adj[n + 1];
	for(int i = 1; i <= n; i++){
		int k;
		cin >> k;
		for(int j = 0; j < k; j++){
			int x;
			cin >> x;
			adj[x].pb(i);
		}
	}
	ll ans = 1e18;
	int par[n + 1];
	for(int i = 1; i <= n; i++){
		memset(par , 0 , sizeof par);
		queue<int> q;
		q.push(i);
		par[i] = -1;
		ll siz[n + 1];
		memset(siz,0,sizeof siz);
		int cnt[n + 1];
		queue<int> q1;
		while(q.size()){
			int node = q.front();
			q.pop();
			int c = 0;
			for(int j : adj[node]){
				if(par[j] == 0){
					par[j] = node;
					q.push(j);
					c++;
				}
			}
			if(c == 0){
				q1.push(node);
			}
			cnt[node] = c;
		}
		bool fine = 1;
		for(int j = 1; j <= n; j++){
			if(par[j] == 0)fine = 0;
		}
		if(fine){
			ll res = 0;
			while(q1.size()){
				int node = q1.front();
				q1.pop();
				siz[node]++;
				res += siz[node];
				if(par[node] == -1)break;
				siz[par[node]] += siz[node];
				cnt[par[node]]--;
				if(cnt[par[node]] == 0)q1.push(par[node]);
			}
			ans = min(ans , res);
		}
	}
	cout << ans << '\n';
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
	//int t;cin >> t;while(t--)
	solve();
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...