problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::count_nodes_with_the_highest_score::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static int countHighestScoreNodes (vector< int > &parents)
 

详细描述

在文件 leetcode.h1634 行定义.

成员函数说明

◆ countHighestScoreNodes()

int leetcode::count_nodes_with_the_highest_score::Solution::countHighestScoreNodes ( vector< int > &  parents)
static

在文件 leetcode.cpp4153 行定义.

4153 {
4154 int ans = 0;
4155 auto nodes = vector<TreeNode *>(parents.size(), nullptr);
4156 auto edges = vector(parents.size(), vector<int>());
4157 auto scores = vector<unsigned long long>(parents.size(), 0);
4158 for(int i = 0; i < parents.size(); i++) {
4159 nodes[i] = new TreeNode(i);
4160 if(parents[i] != -1) {
4161 edges[i].push_back(parents[i]);
4162 edges[parents[i]].push_back(i);
4163 }
4164 }
4165 TreeNode *root = nullptr;
4166 for(int i = 0; i < parents.size(); i++) {
4167 if(parents[i] != -1) {
4168 nodes[parents[i]]->add_child(nodes[i]);
4169 } else {
4170 root = nodes[i];
4171 }
4172 }
4173 root->dfs();
4174 unsigned long long max_score = 0;
4175 for(int i = 0; i < parents.size(); i++) {
4176 unsigned long long score = 1;
4177 const auto *node = nodes[i];
4178 for(const auto *child: node->get_children()) {
4179 score *= child->get_count();
4180 }
4181 if(node->get_parent() != nullptr) {
4182 score *= root->get_count() - node->get_count();
4183 }
4184 max_score = max(max_score, score);
4185 scores[i] = score;
4186 }
4187 return count(scores.begin(), scores.end(), max_score);
4188 }
vector< int > root
Definition: acwing408.cpp:349

引用了 acwing::acwing836_408::root.

被这些函数引用 leetcode::count_nodes_with_the_highest_score::TEST().


该类的文档由以下文件生成: