이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// Always have faith in greedy.
#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<int>> acParents, acChildren;
vector<int> nodes;
vector<int> parent;
vector<vector<int>> adjList;
int r = -1;
int depthFirstSearch(const int& u, const int& p, const int& d)
{
int result = d;
for (auto &v : adjList[u])
result += depthFirstSearch(v, u, d + 1);
return result;
}
int main()
{
// freopen("inp.txt", "r", stdin);
ios::sync_with_stdio(false);
cin >> n;
acParents.resize(n + 1);
acChildren.resize(n + 1);
for (int size, v, u = 1; u <= n; u++)
{
cin >> size;
while (size --> 0)
{
cin >> v;
acParents[u].push_back(v);
acChildren[v].push_back(u);
}
}
nodes.resize(n);
iota(nodes.begin(), nodes.end(), 1);
for (int u = 1; u <= n && r == -1; u++)
if (acParents[u].empty())
r = u;
if (r != -1)
{
swap(nodes[0], nodes[r - 1]);
sort(nodes.begin() + 1, nodes.end(), [&](const int& u, const int& v) -> bool {
return acChildren[u].size() > acChildren[v].size();
});
}
else
{
sort(nodes.begin(), nodes.end(), [&](const int& u, const int& v) -> bool {
return acChildren[u].size() > acChildren[v].size();
});
}
parent.resize(n + 1, -1);
adjList.resize(n + 1);
for (auto &u: nodes)
{
if (r == -1) r = u;
for (auto &v : acChildren[u])
{
if (parent[v] != -1 || v == r) continue;
parent[v] = u;
adjList[u].push_back(v);
}
}
cout << depthFirstSearch(r, -1, 1);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |