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>
#define f first
#define s second
using namespace std;
#define int long long
const int maxn = 5010;
vector<int> g[maxn], t[maxn];
vector<int> vis(maxn), d(maxn);
int ans = 1e18, res;
void dfs(int v) {
for(auto u : t[v]) {
dfs(u);
d[v] += d[u];
}
res += ++d[v];
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while(tt--) {
int n;
cin >> n;
for(int i = 1;i <= n;i++) {
int k;
cin >> k;
for(int j = 1;j <= k;j++) {
int x;
cin >> x;
g[x].push_back(i);
}
}
queue<int> q;
for(int i = 1;i <= n;i++) {
q.push(i);
for(int j = 1;j <= n;j++) {
vis[j] = false;
d[j] = 0;
t[j].clear();
}
vis[i] = 1;
while(!q.empty()) {
auto v = q.front();
q.pop();
for(auto u : g[v]) {
if(vis[u])
continue;
t[v].push_back(u);
q.push(u);
vis[u] = 1;
}
}
res = 0;
dfs(i);
int ok = 1;
for(int j = 1;j <= n;j++)
ok &= (vis[j] == 1);
if(ok)
ans = min(ans, res);
}
cout << ans;
}
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... |