Advertisement
Guest User

Untitled

a guest
May 28th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstdio>
  4.  
  5. using namespace std;
  6.  
  7. class node;
  8. class node{
  9. public:
  10. int el;
  11. int d;
  12. node* next;
  13. node* prev;
  14.  
  15. node(int a, node* p)
  16. {
  17. el=a;
  18. next=NULL;
  19. prev=p;
  20. }
  21. node(int a, int dt, node* p)
  22. {
  23. el=a;
  24. d=dt;
  25. next=NULL;
  26. prev=p;
  27. }
  28. };
  29.  
  30. class node_G{
  31. public:
  32. node* first;
  33. node* last;
  34.  
  35. node_G()
  36. {
  37. first=NULL;
  38. last=NULL;
  39. }
  40. };
  41.  
  42. int jfind(int* j, int a)
  43. {
  44. if(j[a]!=a)
  45. {
  46. return jfind(j, j[a]);
  47. }
  48. else
  49. {
  50. return a;
  51. }
  52. }
  53.  
  54. node_G** push(node_G** g, int A, int B)
  55. {
  56. if(g[A]->last==NULL)
  57. {
  58. g[A]->first=new node(B,NULL);
  59. g[A]->last=g[A]->first;
  60. }
  61. else
  62. {
  63. g[A]->last->next=new node(B,g[A]->last);
  64. g[A]->last=g[A]->last->next;
  65. }
  66. if(g[B]->last==NULL)
  67. {
  68. g[B]->first=new node(A,NULL);
  69. g[B]->last=g[B]->first;
  70. }
  71. else if(A!=B)
  72. {
  73. g[B]->last->next=new node(A,g[B]->last);
  74. g[B]->last=g[B]->last->next;
  75. }
  76. return g;
  77. }
  78.  
  79. node_G** cont(node_G** g, int* j, int* Nv, int A, int B)
  80. {
  81. int Aj=jfind(j, A);
  82. int Bj=jfind(j, B);
  83. if(Aj != Bj && g[Bj]->first!=NULL)
  84. {
  85. g[Aj]->last->next=g[Bj]->first;
  86. g[Aj]->last=g[Bj]->last;
  87. g[Bj]->first=g[Aj]->first;
  88. j[Bj]=Aj;
  89. (*Nv)--;
  90. return g;
  91. }
  92. return g;
  93. }
  94.  
  95. int dist(node_G** g, int Nv, int* j, int A, int B)
  96. {
  97. int ct=0;
  98. int s=jfind(j, A);
  99. int r=jfind(j, B);
  100. int v[Nv];
  101. for(int i=0; i<Nv; i++)
  102. {
  103. v[i]=-1;
  104. }
  105. node_G q;
  106. q.first = new node(s, 0, NULL);
  107. q.last=q.first;
  108. v[s]=ct++;
  109. while(q.first!=NULL)
  110. {
  111. node* a;
  112. node* vert = (node*)malloc(sizeof(node));
  113. vert->el = q.first->el;
  114. vert->next = q.first->next;
  115. vert->d = q.first->d;
  116. node* fwd=q.first->next;
  117. free(q.first);
  118. q.first=fwd;
  119. if(fwd==NULL)
  120. {
  121. q.last = NULL;
  122. }
  123. for (a = g[vert->el]->first; a != NULL; a = a->next)
  124. {
  125. int tp=jfind(j, a->el);
  126. if(r==tp)
  127. {
  128. return vert->d+1;
  129. }
  130. if (v[tp] == -1)
  131. {
  132. v[tp] = ct++;
  133. if(q.last!=NULL)
  134. {
  135. q.last->next= new node(tp, vert->d+1, NULL);
  136. q.last=q.last->next;
  137. }
  138. else
  139. {
  140. q.first = new node(tp, vert->d+1, NULL);
  141. q.last=q.first;
  142. }
  143. }
  144. }
  145. }
  146. return -1;
  147. }
  148.  
  149. int main()
  150. {
  151. ios::sync_with_stdio(false);
  152. cin.tie(NULL);
  153. int N, M, A, B, O, Nv;
  154. char op;
  155. cin >> N >> M;
  156. Nv=N;
  157. int* join = (int*)malloc(sizeof(int)*N);
  158. node_G** adj = (node_G**)malloc(sizeof(node_G*)*N);
  159. for(int i=0; i<N; i++)
  160. {
  161. join[i]=i;
  162. adj[i] = new node_G();
  163. }
  164. for(int i=0; i<M; i++)
  165. {
  166. cin >> A >> B;
  167. adj=push(adj, A, B);
  168. }
  169. cin >> O;
  170. for(int i=0; i<O; i++)
  171. {
  172. cin >> op >> A >> B;
  173. if(op=='c')
  174. {
  175. adj=cont(adj, join, &Nv, A, B);
  176. cout << Nv << "\n";
  177. }
  178. else
  179. {
  180. int wtf=dist(adj, Nv, join, A, B);
  181. cout << wtf << "\n";
  182. }
  183. }
  184. return 0;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement