Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. const int maxN = 10000;
  4. int nodeRootList[1 + maxN];
  5. /*struct node
  6. {
  7. int fatherNum;
  8. };
  9.  
  10. node nodeRootList[1 + maxN];*/
  11.  
  12. namespace algorithm
  13. {
  14. int findRoot(int thisNodeNum)
  15. {
  16. if(nodeRootList[thisNodeNum] == thisNodeNum)
  17. {
  18. return thisNodeNum;
  19. }
  20. else
  21. {
  22. nodeRootList[thisNodeNum] = findRoot(nodeRootList[thisNodeNum]);
  23. return nodeRootList[thisNodeNum];
  24. }
  25. }
  26.  
  27. void mergeNodeRoot(int nodeNumI,int nodeNumJ)
  28. {
  29. int fatherI,fatherJ;
  30. fatherI = findRoot(nodeNumI);
  31. fatherJ = findRoot(nodeNumJ);
  32. if(fatherI != fatherJ)
  33. {
  34. nodeRootList[nodeNumJ] = nodeNumI;
  35. }
  36. }
  37.  
  38. int solve()
  39. {
  40. int N,M;
  41. std::cin >> N >> M;
  42.  
  43. for(int i = 1;i <= N;i++)
  44. {
  45. nodeRootList[i] = i;
  46. }
  47.  
  48. for(int i = 1;i <= M;i++)
  49. {
  50. int Z,X,Y;
  51. std::cin >> Z >> X >> Y;
  52.  
  53. if(Z == 1)
  54. {
  55. mergeNodeRoot(X,Y);
  56. }
  57. if(Z == 2)
  58. {
  59. if(findRoot[X] == findRoot[Y])
  60. {
  61. std::cout << "Y" << std::endl;
  62. }
  63. else
  64. {
  65. std::cout << "N" << std::endl;
  66. }
  67. }
  68. }
  69. }
  70. };
  71.  
  72. int main()
  73. {
  74. algorithm::solve();
  75.  
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement