Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin("graf.in");
  7. ofstream fout("graf.out");
  8.  
  9. void citire();
  10. void DF(int nod);
  11. void ComponenteConexe();
  12. void Init();
  13. void DFciclu(int nod);
  14. void BF(int nod);
  15. int a[20][20],n,i,j,v[20],gasit;
  16. int coada[20],ic,sc;
  17.  
  18. int main()
  19. {
  20. citire();
  21. DF(3);
  22. fout<<endl;
  23. Init();
  24. ComponenteConexe();
  25. fout<<endl;
  26. Init();
  27. BF(6);
  28. Init();
  29. fout<<endl;
  30. DFciclu(1);
  31. if(gasit)
  32. fout<<"Are ciclu";
  33. else
  34. fout<<"Nu are ciclu";
  35.  
  36.  
  37. ;
  38.  
  39.  
  40. return 0;
  41. }
  42.  
  43. void citire()
  44. {
  45. int x,y;
  46. fin>>n;
  47. while(fin>>x>>y)
  48. a[x][y]=a[y][x]=1;
  49. }
  50.  
  51. void DF(int nod)
  52. {
  53. int k;
  54. fout<<nod<<" ";
  55. v[nod]=1;
  56. for(k=1;k<=n;k++)
  57. if(a[nod][k] == 1 && !v[k])
  58. DF(k);
  59. }
  60.  
  61. void ComponenteConexe()
  62. {
  63. int nrcc = 0; ///nr comp. conexe
  64. for(i=1;i<=n;i++)
  65. if(!v[i])
  66. {
  67. nrcc++;
  68. fout<<"Componenta conexa :"<< nrcc <<" :";
  69. DF(i);
  70. }
  71. }
  72.  
  73. void Init()
  74. {
  75. for(i=1;i<=20;i++)
  76. v[i] = 0;
  77. }
  78.  
  79. void DFciclu(int nod)
  80. {
  81. int k;
  82. v[nod] = 1;
  83. for(k=1; k<=n; k++)
  84. if(a[nod][k] == 1)
  85. {
  86. a[k][nod] = 0;
  87. if(!v[k])
  88. DFciclu(k);
  89. else
  90. gasit = 1;
  91. }
  92. }
  93.  
  94. void BF(int nod)
  95. {
  96. sc = ic = 1;
  97. coada[ic] = nod;
  98. v[nod] = 1;
  99. while(ic <= sc)
  100. {
  101. i=1;
  102. while(i<=n)
  103. {
  104. if(a[coada[ic]][i] == 1 && !v[i])
  105. {
  106. sc++;
  107. coada[sc] = i;
  108. v[i] = 1;
  109. }
  110. i++;
  111. }
  112. fout<<coada[ic]<<" ";
  113. ic++;
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement