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;
#define int long long
#define X first
#define Y second
const int N = 5e3 + 1;
vector<int> g[N];
int n, p[N];
int nCh[N];
bool vis[N];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1 ; i <= n ; ++i) {
int s; cin >> s;
while(s--) {
int x; cin >> x;
g[x].push_back(i);
}
}
int ans = 1e18;
for(int i = 1 ; i <= n ; ++i) {
memset(vis,0,sizeof vis);
memset(p,0,sizeof p);
queue<int> q; q.push(i);
vis[i] = 1;
while(q.size()) {
int u = q.front(); q.pop();
for(int v : g[u])
if(!vis[v]) {
p[v] = u;
q.push(v);
vis[v] = 1;
}
}
bool ch = 1;
for(int j = 1 ; j <= n ; ++j) ch &= vis[j];
if (!ch) continue;
int cur = 0;
function<void(int)> dfs = [&](int u) {
nCh[u] = 1;
for(int v : g[u])
if (p[v] == u) {
dfs(v);
nCh[u] += nCh[v];
}
cur += nCh[u];
}; dfs(i);
ans = min(ans,cur);
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |