이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
/*
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("-O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
*/
template <class T> inline T gcd(T a , T b) { return !a ? b : gcd(b % a , a); }
template <class T> inline T lcm(T a , T b) {return (a * b) / gcd(a , b) ; }
#define all(x) x.begin(), x.end()
#define debug(x) { cerr << #x << " = " << x << endl; }
const int N = 5000 + 5;
const int INF = 1e9;
vector<vector<int> > g(N , vector<int>());
int main() {
ios_base :: sync_with_stdio(0);
cin.tie(0) , cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int x;
cin >> x;
g[x].push_back(i);
}
}
auto calc = [&](int root) {
int sz = 1;
queue<int> q;
vector<int> d(n + 1 , 0);
vector<bool> used(n + 1 , false);
d[root] = 0 , used[root] = true , q.push(root);
while (!q.empty()) {
int v = q.front();
q.pop();
for (int to : g[v]) {
if (!used[to]) {
sz++;
used[to] = true;
d[to] = d[v] + 1;
q.push(to);
}
}
}
if (sz != n) {
return INF;
}
int res = 0LL;
vector<int> cnt(n + 1 , 0);
for (int i = 0; i < n; i++) {
cnt[d[i]]++;
}
int children = 0;
for (int i = n; i >= 0; i--) {
res += children + cnt[i];
children += cnt[i];
}
return res;
};
int ans = INF;
for (int i = 1; i <= n; i++) {
ans = min(ans , calc(i));
}
cout << ans << '\n';
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... |