Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1.  
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <vector>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <iostream>
  7. #include <utility>
  8. #include <unordered_map>
  9. #include <stack>
  10. #include <string>
  11. #include <map>
  12. #include <cstring>
  13. #include <algorithm>
  14. #include <set>
  15. #include <cmath>
  16. #include <ctime>
  17. #include <queue>
  18. #include <chrono>
  19. #include <bitset>
  20. #include <unordered_set>
  21. #include <assert.h>
  22. #include <sstream>
  23.  
  24. #include "optimization.h"
  25.  
  26. using namespace std;
  27.  
  28. typedef long long LL;
  29. #define INF 90000000
  30.  
  31. #include <cstdio>
  32.  
  33. #include <cassert>
  34. #include <cstdio>
  35. #include <algorithm>
  36.  
  37. /** Fast allocation /
  38. const int MAX_MEM = 1e8;
  39. int mpos = 0;
  40. char mem[MAX_MEM];
  41. inline void * operator new (size_t n) {
  42. assert((mpos += n) <= MAX_MEM);
  43. return (void *)(mem + mpos - n);
  44. }
  45. inline void operator delete (void *) noexcept { } // must have!
  46. /**/
  47.  
  48. #define MAX_N 20000 + 1
  49.  
  50. using namespace std;
  51.  
  52. static int n, m, ans, color = 1;
  53. stringstream SS;
  54.  
  55. struct trin
  56. {
  57. int id, b, e;
  58.  
  59. trin() {};
  60. trin(int a, int b, int c) : id{ a }, b{ b }, e{ c } {}
  61. };
  62.  
  63.  
  64. bool operator<(const trin &t1, const trin &t2)
  65. {
  66. if (t1.b == t2.b)
  67. if (t1.e == t2.e)
  68. return t1.id < t2.id;
  69. else
  70. return t1.e < t2.e;
  71.  
  72. return t1.b < t2.b;
  73. }
  74.  
  75. typedef pair<int, int> pair_t;
  76.  
  77. set<pair_t> FE[MAX_N];
  78. vector<vector<int>> Components, Paths;
  79. vector<int> Component;
  80. static bool marked[MAX_N];
  81. static int Cnt[MAX_N];
  82. //static int Went[MAX_N * 3 + 10];
  83. unordered_map<int, int> Went;
  84. int id = 0;
  85. stack<int> St;
  86. int dfs_e(int v, int neg)
  87. {
  88. Cnt[v]++;
  89. for (auto e : FE[v])
  90. {
  91. if (!Went[e.first])
  92. {
  93. Went[e.first] = 1;
  94. dfs_e(e.second, e.first >= m);
  95. }
  96. }
  97. St.push(v);
  98. if (neg)
  99. St.push(-1);
  100. return 0;
  101. }
  102.  
  103. int solve_c(vector<int> &C)
  104. {
  105. if (C.size() == 2 || C.size() == 1)
  106. {
  107. Paths.push_back(C);
  108. return 1;
  109. }
  110. int start = C[0], cnt = 0;
  111.  
  112. for (int i = 0; i < C.size(); ++i)
  113. {
  114. int c_i = C[i];
  115. if (FE[c_i].size() % 2)
  116. {
  117. for (int j = i + 1; j < C.size(); ++j)
  118. {
  119. int c_j = C[j];
  120. if (FE[c_j].size() % 2)
  121. {
  122. FE[c_i].insert({ id, c_j });
  123. FE[c_j].insert({ id, c_i });
  124. start = c_i;
  125. cnt++;
  126. id++;
  127. break;
  128. }
  129. }
  130. start = c_i;
  131. }
  132. }
  133. vector<int> Path;
  134.  
  135. if (cnt == 0)
  136. cnt++;
  137. else if (FE[start].size() % 2)
  138. cnt++;
  139. dfs_e(start, 0);
  140.  
  141. int next = St.top();
  142. St.pop();
  143. if (St.top() == -1)
  144. St.pop();
  145. else
  146. Path.push_back(next);
  147. int p = next;
  148. while (St.size())
  149. {
  150. if (St.top() == -1)
  151. {
  152. Paths.push_back(Path);
  153. Path.clear();
  154. }
  155. else if (p != -1 || St.size() > 1)
  156. Path.push_back(St.top());
  157. p = St.top();
  158. St.pop();
  159. }
  160. if (Path.size())
  161. Paths.push_back(Path);
  162. return cnt;
  163. }
  164.  
  165. int main(void)
  166. {
  167. n = readInt();
  168. m = readInt();
  169. id = m;
  170. for (int i = 0; i < m; ++i)
  171. {
  172. int a, b;
  173.  
  174. a = readInt();
  175. b = readInt();
  176. FE[a].insert({ i, b });
  177. FE[b].insert({ i, a });
  178. }
  179.  
  180. for (int i = 1; i <= n; ++i)
  181. Component.push_back(i);
  182.  
  183. int res = 0;
  184. res += solve_c(Component);
  185.  
  186. writeInt(res);
  187. writeChar('\n');
  188.  
  189. for (auto &p : Paths)
  190. {
  191. for (auto v : p)
  192. writeInt(v), writeChar(' ');
  193. writeChar('\n');
  194. }
  195. flush();
  196.  
  197. return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement