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

#include <leetcode.h>

静态 Public 成员函数

static int checkWays (vector< vector< int > > &pairs)
 

详细描述

在文件 leetcode.h1151 行定义.

成员函数说明

◆ checkWays()

int leetcode::number_of_ways_to_reconstruct_a_tree::Solution::checkWays ( vector< vector< int > > &  pairs)
static

在文件 leetcode.cpp2957 行定义.

2957 {
2958 unordered_map<int, unordered_set<int>> adj;
2959 for(auto &p: pairs) {
2960 adj[p[0]].emplace(p[1]);
2961 adj[p[1]].emplace(p[0]);
2962 }
2963 /* 检测是否存在根节点*/
2964 int root = -1;
2965 for(auto &[node, neighbours]: adj) {
2966 if(neighbours.size() == adj.size() - 1) {
2967 root = node;
2968 break;
2969 }
2970 }
2971 if(root == -1) {
2972 return 0;
2973 }
2974
2975 int res = 1;
2976 for(auto &[node, neighbours]: adj) {
2977 if(node == root) {
2978 continue;
2979 }
2980 const int currDegree = neighbours.size();
2981 int parent = -1;
2982 int parentDegree = INT_MAX;
2983
2984 /* 根据 degree 的大小找到 node 的父节点 parent */
2985 for(const auto &neighbour: neighbours) {
2986 if(adj[neighbour].size() < parentDegree && adj[neighbour].size() >= currDegree) {
2987 parent = neighbour;
2988 parentDegree = adj[neighbour].size();
2989 }
2990 }
2991 if(parent == -1) {
2992 return 0;
2993 }
2994
2995 /* 检测 neighbours 是否是 adj[parent] 的子集 */
2996 for(const auto &neighbour: neighbours) {
2997 if(neighbour == parent) {
2998 continue;
2999 }
3000 if(static_cast<unsigned int>(adj[parent].contains(neighbour)) == 0U) {
3001 return 0;
3002 }
3003 }
3004 if(parentDegree == currDegree) {
3005 res = 2;
3006 }
3007 }
3008 return res;
3009 }
vector< int > root
Definition: acwing408.cpp:349

引用了 acwing::acwing836_408::root.

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


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