Advertisement
Guest User

Untitled

a guest
May 20th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. #include<iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. // DSF - disjoint sets forest
  6.  
  7. using namespace std;
  8.  
  9. // element of array
  10. struct Element{
  11. int rank;
  12. int parent;
  13. };
  14.  
  15. // DSF - disjoint sets forest
  16. struct DSF{
  17. Element *arr;
  18. int size;
  19. };
  20.  
  21. void init(DSF & dsf, int size)
  22. {
  23. dsf.size=size;
  24. dsf.arr=new Element[size];
  25. }
  26.  
  27. void makeSet(DSF& dsf,int i)
  28. {
  29. dsf.arr[i].parent=i;
  30. dsf.arr[i].rank=0;
  31. }
  32.  
  33. void makeOneElementSets(DSF & dsf)
  34. {
  35. for(int i=0;i<dsf.size;++i)
  36. makeSet(dsf,i);
  37. }
  38.  
  39.  
  40. int find(DSF & dsf, int index)
  41. {
  42. if(dsf.arr[index].parent!=index)
  43. dsf.arr[index].parent=find(dsf,dsf.arr[index].parent);
  44. return dsf.arr[index].parent; // with path compression
  45. }
  46.  
  47. void link(DSF & dsf, int index1, int index2)
  48. {
  49. if(dsf.arr[index1].rank>dsf.arr[index2].rank)
  50. dsf.arr[index2].parent=index1;
  51. else
  52. dsf.arr[index1].parent=index2;
  53. if(dsf.arr[index1].rank==dsf.arr[index2].rank)
  54. dsf.arr[index2].rank++;
  55. }
  56.  
  57. void makeUnion(DSF & dsf, int index1, int index2)
  58. {
  59. link(dsf,find(dsf,index1),find(dsf,index2));
  60. }
  61.  
  62. int parent(DSF & dsf, int index)
  63. {
  64. return find(dsf,index);
  65. }
  66.  
  67. void showBool(bool val){
  68. if(val)
  69. cout << "true" << endl;
  70. else
  71. cout << "false" << endl;
  72. }
  73.  
  74. bool isCommand(const string command,const char *mnemonic){
  75. return command==mnemonic;
  76. }
  77.  
  78. int main()
  79. {
  80. string line;
  81. string command;
  82. DSF *dsf=NULL;
  83. int currentF=0;
  84. int value;
  85. cout << "START" << endl;
  86. while(true){
  87. getline(cin,line);
  88. std::stringstream stream(line);
  89. stream >> command;
  90. if(line=="" || command[0]=='#')
  91. {
  92. // ignore empty line and comment
  93. continue;
  94. }
  95.  
  96. // copy line on output with exclamation mark
  97. cout << "!" << line << endl;;
  98.  
  99. // change to uppercase
  100. command[0]=toupper(command[0]);
  101. command[1]=toupper(command[1]);
  102.  
  103. // zero-argument command
  104. if(isCommand(command,"HA")){
  105. cout << "END OF EXECUTION" << endl;
  106. break;
  107. }
  108.  
  109. if(isCommand(command,"MO"))
  110. {
  111. makeOneElementSets(dsf[currentF]);
  112. continue;
  113. }
  114.  
  115. // read next argument, one int value
  116. stream >> value;
  117.  
  118. if(isCommand(command,"IN"))
  119. {
  120. init(dsf[currentF],value);
  121. continue;
  122. }
  123.  
  124.  
  125. if(isCommand(command,"FD"))
  126. {
  127. cout << find(dsf[currentF],value) << endl;
  128. continue;
  129. }
  130.  
  131. if(isCommand(command,"PA"))
  132. {
  133. cout << parent(dsf[currentF],value) << endl;
  134. continue;
  135. }
  136.  
  137. if(isCommand(command,"UN"))
  138. {
  139. int value2;
  140. stream >> value2;
  141. makeUnion(dsf[currentF],value,value2);
  142. continue;
  143. }
  144.  
  145.  
  146. if(isCommand(command,"CH"))
  147. {
  148. currentF=value;
  149. continue;
  150. }
  151.  
  152. if(isCommand(command,"GO"))
  153. {
  154. dsf=new DSF[value];
  155. continue;
  156. }
  157.  
  158. cout << "wrong argument in test: " << command << endl;
  159.  
  160. }
  161. return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement