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;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl
using ll = long long;
const ll inf = 1e18;
const int maxn = 1e6 + 5;
int n;
vector<int> g[maxn];
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n;
for (int i=1; i<=n; i++) {
int k;
cin>>k;
while (k--) {
int p;
cin>>p;
g[p].push_back(i);
}
}
ll ans = inf;
for (int r=1; r<=n; r++) {
vector<int> par(n+1,-1);
vector<int> deg(n+1,0);
par[r]=r;
queue<int> qq;
qq.push(r);
while (qq.size()) {
int at=qq.front();
qq.pop();
for (int to: g[at]) {
if (par[to]==-1) {
par[to]=at;
deg[at]++;
qq.push(to);
}
}
}
bool ok = true;
for (int i=1; i<=n && ok; i++) {
if (par[i]==-1) ok=false;
}
if (ok) {
while (qq.size()) qq.pop();
ll cur = 0;
vector<ll> sum(n+1,0);
for (int i=1; i<=n; i++) {
if (deg[i]==0) qq.push(i);
}
while (qq.size()) {
int at = qq.front();
qq.pop();
sum[at]++;
cur += sum[at];
if (par[at]!=at) {
int p=par[at];
sum[p] += sum[at];
if (--deg[p]==0) {
qq.push(p);
}
}
}
ans = min(ans, cur);
}
}
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... |