Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. 2
  2. 3
  3. 4
  4. 5
  5. 6
  6. 7
  7. 8
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 65
  65. 66
  66. 67
  67. 68
  68. 69
  69. 70
  70. 71
  71. 72
  72. 73
  73. 74
  74. 75
  75. 76
  76. 77
  77. 78
  78. #define _CRT_SECURE_NO_WARNINGS
  79. #include <stdio.h>
  80. #include <stdlib.h>
  81.  
  82. typedef struct TNode {
  83. int count_neighbours;
  84. int *neighbours;
  85. int sv_comp;
  86. } TNode;
  87.  
  88. TNode *nodes;
  89. int n;
  90.  
  91. int get_next_vertex() { // libo neproidenoj vershiny, ili vse vershiny proideny
  92. int i;
  93. for (i = 1; i <= n; i++)
  94. if (nodes[i].sv_comp == 0)
  95. return i;
  96. return -1;
  97. }
  98.  
  99. void dfs(int cur, int comp_svyaz)
  100. {
  101. int i, neigh_id;
  102. TNode *neigh;
  103. nodes[cur].sv_comp = comp_svyaz; // tekushej vershine prisvaivaem componentu svyazi
  104. for (i = 0; i < nodes[cur].count_neighbours; i++)
  105. {
  106. neigh_id = nodes[cur].neighbours[i];//berem soseda tekushej vershiny
  107. neigh = &nodes[neigh_id]; //ukazatel na adress vershiny etogo soseda
  108. if (neigh->sv_comp == 0) //ne proideno
  109. dfs(neigh_id, comp_svyaz);
  110. }
  111. }
  112.  
  113. void insert_svyaz(int from, int to)
  114. {
  115. TNode *cur_node = &nodes[from]; // ukazatel na nachal'nuyu vershinu rebra
  116. cur_node->count_neighbours++; //shitaem kolichestvo sosedei
  117. cur_node->neighbours = (int*) realloc(cur_node->neighbours, cur_node->count_neighbours * sizeof(int));//videliaem pamiat na kazhdogo soseda
  118. cur_node->neighbours[cur_node->count_neighbours - 1] = to;
  119. }
  120.  
  121. int main()
  122. {
  123. int i, next_start, cur_comp_svyaz;
  124. freopen("input.txt", "r", stdin);
  125. freopen("output.txt", "w", stdout);
  126. int m;
  127. scanf("%d %d", &n, &m);
  128. nodes = (TNode*) calloc(n+1, sizeof(TNode));
  129. for (i = 0; i < m; i++)
  130. {
  131. int num1, num2;
  132. scanf("%d %d", &num1, &num2);
  133. insert_svyaz(num1, num2);
  134. insert_svyaz(num2, num1);
  135. }
  136. cur_comp_svyaz = 0;
  137. while (1)
  138. {
  139. next_start = get_next_vertex();
  140. if (next_start == -1) break;
  141. cur_comp_svyaz++;
  142. dfs(next_start, cur_comp_svyaz);
  143. }
  144.  
  145. printf("%d\n", cur_comp_svyaz);
  146. for (i = 1; i <= n; i++)
  147. printf("%d ", nodes[i].sv_comp);
  148.  
  149. return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement