Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. using namespace std;
  4.  
  5. struct componenta
  6. {
  7. int nod; /// nodul de val. minima al c.c;
  8. int nrV;
  9. } t[103];
  10.  
  11. int a[103][103], n, nr_CCon;
  12. int viz[103];
  13.  
  14. void creare_MatAd()
  15. {
  16. ifstream fin ("componenteconexe3.in");
  17. fin >> n;
  18.  
  19. int x, y;
  20. while(fin >> x >> y) a[x][y] = a[y][x] = 1;
  21. }
  22.  
  23. void DFS(int k)
  24. {
  25. viz[k] = nr_CCon;
  26. for(int i = 1; i <= n; ++i)
  27. if (!viz[i] && a[i][k])
  28. DFS(i);
  29. }
  30.  
  31. /// Sortez descrescator in functie de nr. de varfuri:
  32. void Sortare()
  33. {
  34. for(int i = 1; i < nr_CCon; ++i)
  35. for(int j = i + 1; j <= nr_CCon; ++j)
  36. if (t[i].nrV < t[j].nrV)
  37. swap(t[i], t[j]);
  38. }
  39.  
  40. void conex()
  41. {
  42. for(int i = 1; i <= n; ++i)
  43. if (!viz[i])
  44. {
  45. ++nr_CCon;
  46. DFS(i);
  47. t[nr_CCon].nod = i;
  48. }
  49.  
  50. /// Determin cate varfuri are fiecare comp. conexa:
  51. for(int i = 1; i <= n; ++i)
  52. ++t[viz[i]].nrV;
  53.  
  54. Sortare();
  55. }
  56.  
  57. int main()
  58. {
  59. creare_MatAd();
  60. conex();
  61.  
  62. ofstream fout ("componenteconexe3.out");
  63. fout << t[1].nod << " " << t[1].nrV;
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement