This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/***************************************************************************/
/********************** LANG TU HAO HOA **********************************/
/***************************************************************************/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define sz(x) ((int) x.size())
#define PB push_back
#define PF push_front
#define MP make_pair
#define ll long long
#define F first
#define S second
#define maxc 1000000007
#define MOD 1000000007
#define base 107
#define eps 1e-6
#define pi acos(-1)
#define N 5003
#define K 10004
#define task ""
#define remain(x) ((x > MOD) ? (x - MOD) : x)
using namespace std;
int n, f[N], cnt = 0, in[N], ans = maxc;
pii canh[K];
vector <int> ke[N];
void DFS(int u)
{
f[u] = 1;
for (auto v : ke[u])
{
DFS(v);
f[u] += f[v];
}
}
void Sub1()
{
int res = maxc;
int Last = (1 << cnt) - 1;
FOR(x, 0, Last)
{
if (__builtin_popcount(x) != n-1) continue;
FOR(i, 1, n) ke[i].clear(), in[i] = 0, f[i] = 0;
FOR(i, 1, cnt)
{
if ((x >> (i-1)) & 1)
{
ke[canh[i].F].PB(canh[i].S);
in[canh[i].S]++;
}
}
int root, sl = 0;
FOR(i, 1, n)
{
if (!in[i])
{
root = i;
sl++;
}
}
if (sl > 1) continue;
DFS(root);
if (*min_element(f+1, f+n+1) == 0) continue;
res = min(res, accumulate(f+1, f+n+1, 0));
}
cout << res;
exit(0);
}
void Try(int root)
{
fill(f+1, f+n+1, 0);
int res = 0;
queue <pii> q;
q.push(MP(root, 1));
while (!q.empty())
{
int u = q.front().F;
int curH = q.front().S;
q.pop();
if (f[u]) continue;
f[u] = 1;
res += curH;
for (auto v : ke[u])
{
if (f[v]) continue;
q.push(MP(v, curH+1));
}
}
if (accumulate(f+1, f+n+1, 0) == n) ans = min(res, ans);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
//freopen(task".inp", "r", stdin);
//freopen(task".out", "w", stdout);
cin >> n;
FOR(i, 1, n)
{
int k; cin >> k;
FOR(j, 1, k)
{
int x; cin >> x;
canh[++cnt] = MP(x, i);
}
}
if (n <= 10) Sub1();
FOR(i, 1, cnt) ke[canh[i].F].PB(canh[i].S);
FOR(i, 1, n) Try(i);
cout << ans;
return 0;
}
Compilation message (stderr)
bosses.cpp: In function 'void Sub1()':
bosses.cpp:69:12: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
DFS(root);
~~~^~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |