Advertisement
a53

ComponenteConexe5

a53
Feb 25th, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. ifstream fin("componenteconexe5.in");
  6. ofstream fout("componenteconexe5.out");
  7.  
  8. int n , m , A[1001][1001], Dim[1001], CC[1001], nrc, nrv;
  9.  
  10. void DFS(int x , int nrc)
  11. {
  12. CC[x] = nrc;
  13. Dim[nrc] ++;
  14. for(int i = 1 ; i <= n ; i ++)
  15. if(A[x][i] == 1 && CC[i] == 0)
  16. DFS(i , nrc);
  17. }
  18.  
  19. int main()
  20. {
  21. int i , j;
  22. fin >> n >> m;
  23. while(m)
  24. {
  25. fin >> i >> j;
  26. A[i][j] = A[j][i] = 1;
  27. m --;
  28. }
  29. nrc = 0;
  30. for(int i = 1 ; i <= n ; i ++)
  31. if(CC[i] == 0)
  32. {
  33. nrc ++;
  34. DFS(i , nrc);
  35.  
  36. }
  37. int q , x;
  38. fin >> q;
  39. while(q)
  40. {
  41. fin >> x;
  42. fout << Dim[CC[x]] << '\n';
  43. q --;
  44. }
  45. return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement