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;
typedef long long ll;
typedef pair<int,int> pp;
typedef pair<ll,ll> pll;
void cppio(){ ios_base::sync_with_stdio(0); cin.tie(0); }
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define sz(x) (int)(x).size()
#define XY(p) p.x, p.y
int n;
vector<int> e[5010];
bitset<5001> vis;
ll s[5010];
ll d[5010];
int q[5010];
int par[5010];
const ll inf = 1ll<<60;
ll f(int r)
{
vis.reset(); vis[r] = 1;
int head = 1, tail = 0; q[0] = r; par[r] = -1;
while(head>tail) {
int x = q[tail++];
for (int y:e[x]) if (!vis[y]) vis[y]=1, par[y]=x, q[head++]=y;
}
if (head != n) return inf;
for (int i=n-1; 0<=i; --i) {
int x = q[i]; s[x] = 1; d[x] = 0;
for (int y:e[x]) if (par[y] == x) s[x] += s[y], d[x] += d[y];
d[x] += s[x];
}
return d[r];
}
int main()
{
cppio(); cin >> n;
rrep(i, n) {
int k; cin >> k;
for(;k--;) {
int j; cin >> j;
e[j].pb(i);
}
}
ll ans = inf;
rrep(i, n) ans = min(ans, f(i));
cout << ans << endl;
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... |