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 MOD 1000000007
#define INF 1061109567
#define INFLL 4557430888798830399
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s,"r",stdin);
#define out(s) freopen(s,"w",stdout);
#define fi first
#define se second
#define bw(i,r,l) for (int i=r-1;i>=l;i--)
#define fw(i,l,r) for (int i=l;i<r;i++)
#define fa(i,x) for (auto i:x)
using namespace std;
const int N = 5005;
int n, sz[N];
vector<int> g[N], child[N];
bool used[N];
int dfs(int u) {
sz[u] = 1;
int ans = 0;
fa (v, child[u]) {
ans += dfs(v);
sz[u] += sz[v];
}
ans += sz[u];
return ans;
}
signed main() {
#ifdef BLU
in("blu.inp");
#endif
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
fw (i, 0, n) {
int k;
cin >> k;
while (k--) {
int x;
cin >> x;
x--;
g[x].pb(i);
}
}
/*
Salary of person u is equal to the number of nodes inside the subtree
Fix root as r. At each step just BFS into all possible choices.
*/
int res = INF;
fw (r, 0, n) {
queue<int> q;
fw (i, 0, n) child[i].clear();
q.push(r);
int lft = n;
memset(used, 0, sizeof used);
used[r] = 1;
while (!q.empty()) {
lft--;
int u = q.front(); q.pop();
fa (v, g[u]) if (!used[v]) {
used[v] = 1;
child[u].pb(v);
q.push(v);
}
}
int ans = dfs(r);
if (!lft) res = min(res, ans);
}
cout << res;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |