#include <bits//stdc++.h>
using namespace std;
#define ll long long
#define st short
#define iloop(m, h) for (auto i = m; i != h; i+=(m<h?1:-1))
#define jloop(m, h) for (auto j = m; j != h; j+=(m<h?1:-1))
#define kloop(m, h) for (auto k = m; k != h; k+=(m<h?1:-1))
#define lloop(m, h) for (auto l = m; l != h; l+=(m<h?1:-1))
#define iloop_(m, h, g) for (auto i = m; i != h; i+=g)
#define jloop_(m, h, g) for (auto j = m; j != h; j+=g)
#define kloop_(m, h, g) for (auto k = m; k != h; k+=g)
#define lloop_(m, h, g) for (auto l = m; l != h; l+=g)
#define getchar_unlocked _getchar_nolock // comment before submission
#define pll pair<ll,ll>
#define plll pair<ll, pll>
#define pllll pair<pll, pll>
#define vll vector<ll>
#define qll queue<ll>
#define dll deque<ll>
#define pqll priority_queue<ll>
#define gll greater<ll>
#define INF 1000000000000000
#define MOD1 1000000007
#define MOD2 998244353
#define MOD3 1000000009
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
ll n, t1, t2, v;
bool vis[5005];
vll adj[5005];
ll ans[5005];
vector<vll> vv;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
iloop(0, n) {
cin >> t1;
jloop(0, t1) {
cin >> t2;
adj[t2-1].push_back(i);
}
}
t1 = INF;
qll q;
ll fin, cn;
ll par[n];
iloop(0, n) {
jloop(0, n) {
vis[j] = 0;
ans[j] = 1;
}
fin = 0;
vv.clear();
vv.push_back({i});
vis[i] = 1;
par[i] = i;
q.push(i);
cn = 0;
while (q.size()) {
vv.push_back({});
while (q.size()) {
ll nd = q.front();
q.pop();
cn++;
for (auto it : adj[nd]) if (!vis[it]) {vis[it] = 1; par[it] = nd; vv.back().push_back(it);}
}
for (auto it : vv.back()) q.push(it);
}
if (cn != n) continue;
reverse(vv.begin(), vv.end());
for (auto it : vv) {
for (auto it2 : it) {
fin += ans[it2];
ans[par[it2]] += ans[it2];
}
}
t1 = min(t1, fin);
}
cout << t1;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |