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() , 以及 root.

被这些函数引用 find(), 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)

引用了 find(), merge() , 以及 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(), merge() , 以及 root.

被这些函数引用 main() , 以及 merge().

◆ 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(istream &cin, ostream &cout)

引用了 main().

变量说明

◆ root

vector<int> acwing::acwing836_408::root

在文件 acwing408.cpp349 行定义.

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