problemscpp
A collection of my answers to algorithm problems in c++.
函数 | 变量
acwing::acwing836_408 命名空间参考

函数

int find (int x)
 
int main (istream &cin, ostream &cout)
 
void merge (int x, int y)
 
 TEST (acwing836_408, case1)
 

变量

vector< int > root
 

函数说明

◆ find()

int acwing::acwing836_408::find ( int  x)

在文件 acwing408.cpp351 行定义.

351 {
352 if(root[x] < 0)
353 return x;
354 else {
355 int grand_parent = find(root[x]);
356 root[x] = grand_parent;
357 return grand_parent;
358 }
359 }

引用了 find() , 以及 acwing::acwing3786::root.

被这些函数引用 find(), main(), acwing::acwing848_408::main(), acwing::acwing3397::main() , 以及 merge().

◆ main()

int acwing::acwing836_408::main ( istream &  cin,
ostream &  cout 
)

在文件 acwing408.cpp375 行定义.

375 {
376 int n, m;
377 cin >> n >> m;
378 root = vector<int>(n + 1, -1);
379 char op;
380 int a, b;
381 while(m--) {
382 cin >> op >> a >> b;
383 if(op == 'M') {
384 merge(a, b);
385 } else {
386 cout << (find(a) == find(b) ? "Yes" : "No") << endl;
387 }
388 }
389 return 0;
390 }
void merge(int x, int y)
Definition: acwing408.cpp:361

引用了 find(), merge() , 以及 acwing::acwing3786::root.

被这些函数引用 TEST().

◆ merge()

void acwing::acwing836_408::merge ( int  x,
int  y 
)

在文件 acwing408.cpp361 行定义.

361 {
362 int root_x = find(x);
363 int root_y = find(y);
364 if(root_x != root_y) {
365 if(root[root_x] < root[root_y]) {
366 root[root_x] += root[root_y];
367 root[root_y] = root_x;
368 } else {
369 root[root_y] += root[root_x];
370 root[root_x] = root_y;
371 }
372 }
373 }

引用了 find() , 以及 acwing::acwing3786::root.

被这些函数引用 main().

◆ TEST()

acwing::acwing836_408::TEST ( acwing836_408  ,
case1   
)

在文件 acwing408_test.cpp948 行定义.

948 {
949 istringstream in("4 5\n"
950 "M 1 2\n"
951 "M 3 4\n"
952 "Q 1 2\n"
953 "Q 1 3\n"
954 "Q 3 4");
955 auto out = ostringstream();
956 main(in, out);
957 const auto ans = out.str();
958 ASSERT_EQ("Yes\n"
959 "No\n"
960 "Yes\n",
961 ans);
962 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().

变量说明

◆ root

vector<int> acwing::acwing836_408::root

在文件 acwing408.cpp349 行定义.

被这些函数引用 leetcode::number_of_ways_to_reconstruct_a_tree::Solution::checkWays(), leetcode::populating_next_right_pointers_in_each_node_ii::Solution::connect(), leetcode::convert_bst_to_greater_tree::Solution::convertBST(), leetcode::count_nodes_with_the_highest_score::Solution::countHighestScoreNodes(), leetcode::delete_node_in_a_bst::Solution::deleteNode(), leetcode::serialize_and_deserialize_binary_tree::Codec::deserialize(), leetcode::concatenated_words::TrieNode::dfs(), leetcode::all_nodes_distance_k_in_binary_tree::Solution::distanceK(), leetcode::two_sum_iv_input_is_a_bst::Solution::findTarget(), lintcode::flatten::Solution::flatten(), pat::a::a7_4::genTree(), leetcode::UhWRSj::TrieNode::get_prefix(), leetcode::find_all_the_lonely_nodes::Solution::getLonelyNodes(), leetcode::subtree_of_another_tree::Solution::isSubtree(), leetcode::n_ary_tree_level_order_traversal::Solution::levelOrder(), leetcode::longest_word_in_dictionary::Solution::longestWord(), leetcode::lowest_common_ancestor_of_a_binary_search_tree::Solution::lowestCommonAncestor(), pat::a::a1020::main(), pat::a::a7_4::main(), lintcode::min_diff_in_BST::Solution::minDiffInBST(), pat::a::a1020::parse(), leetcode::n_ary_tree_postorder_traversal::Solution::postorder(), leetcode::n_ary_tree_preorder_traversal::Solution::preorder(), leetcode::UhWRSj::Solution::replaceWords(), leetcode::house_robber_iii::Solution::rob(), leetcode::serialize_and_deserialize_binary_tree::Codec::serialize(), leetcode::delete_node_in_a_bst::TEST(), leetcode::lowest_common_ancestor_of_a_binary_search_tree::TEST(), leetcode::serialize_and_deserialize_binary_tree::TEST() , 以及 leetcode::construct_string_from_binary_tree::Solution::tree2str().