이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
/*
S[i] = sum{j in children[i], S[j]} + 1
=> S[leaf] = 0
Every non-leaf i contributes depth[i] to the total sum by increasing each of its ancestors and itself by 1
So, answer = sum{i not a leaf, depth[i]}
Do a BFS to find minimum depths. Try out every vertex as root once.
*/
int main()
{
int n;
cin >> n;
vector<int> next[n+1];
int a, b;
for(int i = 1; i <= n; i++)
{
cin >> a;
for(int j = 1; j <= a; j++)
{
cin >> b;
next[b].push_back(i);
}
}
int res = 2000000000, r;
vector<int> S(n+1);
queue<int> tbv, dist;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++) S[j] = 0;
S[i] = 1;
r = 1;
tbv.push(i);
dist.push(1);
while(!tbv.empty())
{
for(int j:next[tbv.front()])
{
if(S[j]) continue;
S[j] = dist.front() + 1;
r += dist.front() + 1;
dist.push(dist.front() + 1);
tbv.push(j);
}
tbv.pop();
dist.pop();
}
for(b = 1; b <= n; b++) if(S[b] == 0) break;
if(b <= n) continue;
res = min(res, r);
}
cout << res << '\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... |