Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int a[101][101], b[101][101], n, viz[101], nrc;
  6.  
  7. ifstream fin ("componenteconexe.in");
  8. ofstream fout ("componenteconexe.out");
  9.  
  10. void Citire()
  11. {
  12. int i, j;
  13. fin >> n;
  14. while(fin >> i >> j)
  15. a[i][j] = a[j][i] = 1;
  16. }
  17.  
  18. void DFS(int k)
  19. {
  20. viz[k] = 1;
  21. b[nrc][k] = 1;
  22. for(int i = 1; i <= n; i++)
  23. if(a[k][i] && viz[i] == 0)
  24. DFS(i);
  25. }
  26.  
  27. int main()
  28. {
  29. int x, y, i, j;
  30. fin >> n;
  31. while(fin >> x >> y)
  32. a[x][y] = a[y][x] = 1;
  33. for(i = 1; i <= n; i++)
  34. if(!viz[i])
  35. {
  36. nrc++;
  37. DFS(i);
  38. }
  39. fout << nrc << "\n";
  40. for(i = 1; i <= nrc; i++)
  41. {
  42. for(j = 1; j <= n; j++)
  43. if(b[i][j] == 1)
  44. fout << j << " ";
  45. fout <<"\n";
  46. }
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement