Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <string>
  5. #include <cassert>
  6. #include <queue>
  7.  
  8. using namespace std;
  9.  
  10. class Graph {
  11. public:
  12. vector<vector<int>> data;
  13.  
  14. Graph(const int x) : data(x, vector<int>(x, 0)) {};
  15.  
  16. Graph(vector<vector<int>> x) : data(std::move(x)) {};
  17.  
  18. friend ostream &operator<<(ostream &out, const Graph &x) {
  19. for (int i = 0; i < x.data.size(); ++i) {
  20. for (int j = 0; j < x.data.size(); ++j) {
  21. out << x.data[i][j] << ' ';
  22. }
  23. out << '\n';
  24. }
  25. return out;
  26. }
  27.  
  28. void AddVertice() {
  29. for (auto &i : data) {
  30. i.push_back(0);
  31. }
  32. data.emplace_back(data.size() + 1, 0);
  33. }
  34.  
  35. void DeleteVertice(const int &x) {
  36. assert(x < data.size());
  37. auto temp = vector<vector<int>>(data.size() - 1, vector<int>(data.size() - 1));
  38. int i1 = 0, j1 = 0;
  39. for (int i = 0; i < data.size() && i != x; ++i) {
  40. if (i != x) {
  41. for (int j = 0; j < data.size(); ++j) {
  42. if (j != x) {
  43. temp[i1][j1] = data[i][j];
  44. ++j1;
  45. }
  46. }
  47. ++i1;
  48. }
  49. }
  50. data = temp;
  51. }
  52.  
  53. void AddEdge(const int &a, const int &b) {
  54. assert(a != b);
  55. data[a][b] = 1;
  56. data[b][a] = 1;
  57. }
  58.  
  59. void DeleteEdge(const int &a, const int &b) {
  60. data[a][b] = 0;
  61. data[b][a] = 0;
  62. }
  63.  
  64. int UsedVertices() {
  65. int count = 0;
  66. for (int i = 0; i < data.size(); ++i) {
  67. for (int j = 0; j < data.size(); ++j) {
  68. if (data[i][j] == 1)
  69. ++count;
  70. }
  71. }
  72. return count / 2;
  73. }
  74.  
  75. bool IsCycled() {
  76. vector<bool> used(data.size(), false);
  77. return DfsCycle(used, 0, -1);
  78. }
  79.  
  80. bool DfsCycle(vector<bool> &visited, int x, int p) {
  81. visited[x] = true;
  82. for (int i = 0; i < visited.size(); ++i) {
  83. if (data[x][i] == 1 && i != p) {
  84. if (visited[i] == true) {
  85. return true;
  86. }
  87. bool q = DfsCycle(visited, i, x);
  88. if (q == true) {
  89. return q;
  90. }
  91. }
  92. }
  93. return false;
  94. }
  95.  
  96. vector<int> Conherence() {
  97. vector<int> g(data.size(), -1);
  98. int index = 0;
  99. for (int i = 0; i < data.size(); ++i) {
  100. if (g[i] == -1) {
  101. DfsConherence(g, i, -1, index);
  102. ++index;
  103. }
  104. }
  105. g.push_back(index);
  106. return g;
  107. }
  108.  
  109. void DfsConherence(vector<int> &g, int a, int b, int index) {
  110. g[a] = index;
  111. for (int i = 0; i < g.size(); ++i) {
  112. if (data[a][i] == 1 && i != b) {
  113. DfsConherence(g, i, a, index);
  114. }
  115. }
  116. }
  117.  
  118. bool IsTree() {
  119. bool q1 = IsCycled();
  120. vector<int> q2 = Conherence();
  121. return !IsCycled() && q2[q2.size() - 1] == 1;
  122. }
  123.  
  124. // int Len(int a, int b) {
  125. // assert(a != b);
  126. // vector<int> l = Conherence();
  127. // assert(l[a] == l[b]);
  128. // vector<int> d (data.size(), 10000), p (data.size());
  129. // vector<bool> u (data.size());
  130. // d[a] = 0;
  131. // for (int i = 0; i < data.size(); ++i) {
  132. // int v = -1;
  133. // for (int j = 0; j < data.size(); ++j) {
  134. // if (!u[j] && (v == -1 || d[j] < d[v]))
  135. // v = j;
  136. // }
  137. // if (d[v] == 10000)
  138. // break;
  139. // u[v] = true;
  140. // for (int j = 0; j < data.size(); ++j) {
  141. // if (data[i][j] == 1) {
  142. // if (d[v] + 1 < d[j]){
  143. // d[j] = d[v] + 1;
  144. // p[j] = v;
  145. // }
  146. // }
  147. // }
  148. // }
  149. // return d[4];
  150. // }
  151.  
  152. int Len(int a, int b) {
  153. assert(a != b);
  154. vector<int> l = Conherence();
  155. assert(l[a] == l[b]);
  156. queue<int> q;
  157. vector<int> d(data.size(), 1000);
  158. q.push(a);
  159. d[a] = 0;
  160. while (q.size() != 0) {
  161. int temp = q.front();
  162. q.pop();
  163. for (int i = 0; i < data.size(); ++i) {
  164. if (data[temp][i] == 1) {
  165. if (d[i] == 1000) {
  166. d[i] = d[temp] + 1;
  167. q.push(i);
  168. }
  169. }
  170. }
  171. }
  172. return d[b];
  173.  
  174. }
  175.  
  176. };
  177.  
  178. //class Tree: public Graph {
  179. //
  180. // void AddTree(Tree &a, Graph &b, int na, int nb) {
  181. // assert(b.IsTree());
  182. //
  183. // }
  184. //};
  185.  
  186.  
  187. int main() {
  188. vector<vector<int>> a(5, vector<int>(5, 0));
  189. a[0][1] = 1; a[0][2] = 0; a[0][3] = 1; a[0][4] = 0;
  190. a[1][0] = 1; a[1][2] = 0; a[1][3] = 0; a[1][4] = 0;
  191. a[2][0] = 0; a[2][1] = 0; a[2][3] = 1; a[2][4] = 0;
  192. a[3][0] = 1; a[3][1] = 0; a[3][2] = 1; a[3][4] = 1;
  193. a[4][0] = 0; a[4][1] = 0; a[4][2] = 0; a[4][3] = 1;
  194. Graph s = Graph(a);
  195. vector<int> f = s.Conherence();
  196.  
  197. cout << s.Len(2,4);
  198.  
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement