sahadat49

uva 567

Mar 28th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int>vec[1000];
  4. int level[1000];
  5. void bfs(int s,int d)
  6. {
  7. int vis[1000]= {0};
  8. queue<int>q;
  9. queue<int>q1;
  10. vis[s]=1;
  11. level[s]=0;
  12. q.push(s);
  13. q1.push(s);
  14. while(!q.empty ())
  15. {
  16. int x=q.front();
  17.  
  18. cout<<"X = "<<x<<endl;
  19.  
  20. for(int l=0; l<vec[x].size(); l++)
  21. {
  22. int y=vec[x][l];
  23.  
  24. if(vis[y]==0)
  25. {
  26. vis[y]=1;
  27. level[y]=level[x]+1;
  28. cout<<"l = "<<l<<endl;
  29. cout<<"Level of "<<y<<" = "<<level[y]<<endl;
  30. q.push(y);
  31. }
  32.  
  33. /*loop ses na koria baroy khene ? */
  34.  
  35. }
  36. cout<<"Baroise"<<endl;
  37. q.pop();
  38. }
  39. printf("%2d to %2d: %d\n", s, d, level[d]);
  40. }
  41.  
  42.  
  43. int main()
  44. {
  45. int p,q,r,s,h,c=0;
  46. while(cin>>p)
  47. {
  48. for(int i=0; i<p; i++)
  49. {
  50. cin>>q;
  51. vec[1].push_back(q);
  52. vec[q].push_back(1);
  53. }
  54.  
  55. for(int j=2; j<20; j++)
  56. {
  57. cin>>r;
  58. for(int i=0; i<r; i++)
  59. {
  60. cin>>h;
  61. vec[j].push_back(h);
  62. vec[h].push_back(j);
  63. }
  64. }
  65. cin>>s;
  66. printf("Test Set #%d\n",++c);
  67. int s1,s2;
  68. for(int k=0; k<s; k++)
  69. {
  70. cin>>s1>>s2;
  71. bfs(s1,s2);
  72. }
  73. cout<<endl;
  74. for(int i=0; i<=20; i++)
  75. {
  76. vec[i].clear();
  77. }
  78. }
  79. return 0;
  80. }
  81.  
  82. //test case
  83. /*
  84.  
  85. 1 3
  86. 2 3 4
  87. 3 4 5 6
  88. 1 6
  89. 1 7
  90. 2 12 13
  91. 1 8
  92. 2 9 10
  93. 1 11
  94. 1 11
  95. 2 12 17
  96. 1 14
  97. 2 14 15
  98. 2 15 16
  99. 1 16
  100. 1 19
  101. 2 18 19
  102. 1 20
  103. 1 20
  104. 5
  105. 1 20
  106. 2 9
  107. 19 5
  108. 18 19
  109. 16 20
  110. 4 2 3 5 6
  111. 1 4
  112. 3 4 10 5
  113. 5 10 11 12 19 18
  114. 2 6 7
  115. 2 7 8
  116. 2 9 10
  117. 1 9
  118. 1 10
  119. 2 11 14
  120. 3 12 13 14
  121. 3 18 17 13
  122. 4 14 15 16 17
  123. 0
  124. 0
  125. 0
  126. 2 18 20
  127. 1 19
  128. 1 20
  129. 6
  130. 1 20
  131. 8 20
  132. 15 16
  133. 11 4
  134. 7 13
  135. 2 16
  136.  
  137. */
Advertisement
Add Comment
Please, Sign In to add comment