Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4.  
  5.  
  6.  
  7. using namespace std;
  8.  
  9.  
  10. ifstream in("componenteconexe.in");
  11. ofstream out("componenteconexe.out");
  12.  
  13.  
  14. int v[101][101],viz[101],n,c[101],d=1;
  15.  
  16.  
  17. void DFS(int nod,int nc)
  18. {
  19. int i;
  20. viz[nod]=nc;
  21.  
  22.  
  23. for(i=1 ; i<=n ; i++)
  24. if(v[nod][i]==1 && !viz[i])
  25.  
  26. DFS(i,nc);
  27.  
  28.  
  29. }
  30.  
  31.  
  32.  
  33.  
  34. int main()
  35. {
  36. int m,x,i,a,b,e=0,h=1,j=1;
  37. in>>n;
  38.  
  39. while(in>>a>>b)
  40. {
  41. v[a][b]=v[b][a]=1;
  42. x=a;
  43. }
  44.  
  45. int p=1,nod,coada[101],nc=1;
  46.  
  47. for(i=1 ; i<=n ; i++)
  48. {
  49. if(viz[i]==0)
  50. {
  51.  
  52.  
  53. DFS(i,nc);
  54. nc++;
  55.  
  56. }
  57. }
  58.  
  59. nc--;
  60. out<<nc<<"\n";
  61.  
  62.  
  63. for (int i=1; i<=nc; i++)
  64. {
  65. for (int j=1; j<=n; j++)
  66. {
  67. if (viz[j] == i)
  68. {
  69. out << j << " ";
  70. }
  71. }
  72. out << "\n";
  73. }
  74.  
  75.  
  76. return 0;
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement