Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. ///Se citesc informatiile unui graf neorientat din fisierul graf.in: n, m 2 numere naturate, 2<=n,m<=100,
  2. /// n numarul de noduri si m perechi de muchii. Afisati toate componentele de lungima maxima care contin
  3. ///cel putin un nod de grad minim diferit de 0. Afisarile se vor efectua in fisierul graf.out.
  4. ///Rezolvarea problemei se va efectua cu functii.
  5. #include <iostream>
  6. #include <fstream>
  7. using namespace std;
  8. int n,m,a[101][101],viz[101],gr[101],bun;
  9. void citire()
  10. {
  11. ifstream f("grad.in");
  12. f>>n>>m;
  13. for(int i=1; i<=n; i++)
  14. {
  15. int x,y;
  16. a[x][y]=a[y][x]=1;
  17. gr[x]++;
  18. gr[y]++;
  19. }
  20.  
  21.  
  22. }
  23. int bfs(int start,int componenta)
  24. {
  25. int coada[101];
  26. bun=0;
  27. int u=1,p=0;
  28. coada[p]=start;
  29. viz[start]=componenta;
  30. if(gr[start]!=0)
  31. bun=1;
  32. while(p<=u)
  33. {
  34. int nod=coada[p];
  35. for(int i=1; i<=n; ++i)
  36. if(a[nod][i]==1 && viz[i]==0)
  37. {
  38. viz[i]=1;
  39. coada[++u]=i;
  40. if(gr[i]!=0)
  41. bun=1;
  42. }
  43. }
  44. return u;
  45.  
  46. }
  47. void afisare()
  48. {
  49. ofstream g("graf.out");
  50. int nrnodurimax=1,componentamax[101],componente=1;
  51. int k=0,ok=0;
  52. for(int i=1; i<=n; ++i)
  53. if(viz[i]==0)
  54. {
  55. int h=bfs(i,componente);
  56. if(h>nrnodurimax && bun==1)
  57. k=0,nrnodurimax=h;
  58. else
  59. if(h==nrnodurimax && bun==1)
  60. componentamax[k++]=componente;
  61. componente++;
  62.  
  63. }
  64. for(int i=0;i<k;i++)
  65. {
  66. int ok=0;
  67. for(int j=1;i<=n;j++)
  68. {
  69. g<<j<<' ';
  70. }
  71.  
  72. }
  73. if(ok==1)
  74. g<<endl;
  75.  
  76.  
  77.  
  78. }
  79. int main()
  80. {
  81. citire();
  82. afisare();
  83.  
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement