Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
find parent count of node in graph
#include <iostream> #include <vector> #include <queue> #define LOG(_EXPR) { std::cout << #_EXPR " is " << (_EXPR) << "\n"; } struct Node { int id; std::vector<int> children; Node(int _id) : id(_id) {} Node(int _id, std::vector<int>&& _children) : id(_id), children(std::move(_children)) {} }; template <typename T> bool vec_contains(std::vector<T> vec, const T& val) { for (const T& vec_val : vec) if (vec_val == val) return true; return false; } int find_parent_count_of(const std::vector<Node>& nodes, int child_id, std::vector<int>& _parent_passed, unsigned int _parent_pos) { int count = 0; for (const auto& node : nodes) { for (int node_child_id : node.children) { if (child_id == node_child_id && !vec_contains(_parent_passed, node.id)) { _parent_passed.push_back(node.id); count++; } } } _parent_pos++; if (_parent_pos < _parent_passed.size()) count += find_parent_count_of(nodes, _parent_passed[_parent_pos], _parent_passed, _parent_pos); return count; } int find_parent_count_of(const std::vector<Node>& nodes, int child_id) { std::vector<int> _parent_passed; return find_parent_count_of(nodes, child_id, _parent_passed, 0); } int main() { std::vector<Node> nodes; nodes.push_back(Node(7, {11, 8 })); nodes.push_back(Node(5, {11 })); nodes.push_back(Node(3, {8, 10 })); nodes.push_back(Node(11, {2, 8, 10})); nodes.push_back(Node(8, {9 })); nodes.push_back(Node(2)); nodes.push_back(Node(9)); nodes.push_back(Node(10)); LOG(find_parent_count_of(nodes, 3)); LOG(find_parent_count_of(nodes, 11)); LOG(find_parent_count_of(nodes, 10)); }
run
|
edit
|
history
|
help
0
My First Wall
proga2
new
Permute
project
Q
Mr
Size of data type test
in shorted array print without duplicate o/p array
bmp_lb