Guest User

Untitled

a guest
Apr 10th, 2021
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define pb push_back
  5. #define ld long double
  6. #define int long long
  7. #define ll __int128
  8.  
  9. mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
  10.  
  11. const int N = 1e5 + 10;
  12. int was[N], mt[N], used[N];
  13. vector<pair<int,int>> show;
  14. vector<int> now, g[N];
  15. int cnt = 1;
  16.  
  17. bool dfs(int v, int now){
  18. if (used[v] == now) return 0;
  19. used[v] = now;
  20. for (auto to : g[v]){
  21. if (mt[to] == -1) {
  22. mt[to] = v;
  23. if (mt[v] != -1) mt[mt[v]] = -1;
  24. mt[v] = to;
  25. return 1;
  26. }
  27. }
  28. for (auto to : g[v]){
  29. used[to] = now;
  30. if (dfs(mt[to], now)){
  31. if (mt[to] != -1) mt[mt[to]] = -1;
  32. mt[to] = v;
  33. if (mt[v] != -1) mt[mt[v]] = -1;
  34. mt[v] = to;
  35. return 1;
  36. }
  37. }
  38. return 0;
  39. }
  40.  
  41.  
  42. void solve(vector<int> a){
  43. for (auto i : a) mt[i] = -1;
  44. for (auto i : a){
  45. shuffle(g[i].begin(), g[i].end(), rnd);
  46. }
  47. for (int i = 0; i < a.size(); i++){
  48. for (auto j : a){
  49. if (mt[j] == -1){
  50. dfs(j, cnt++);
  51. }
  52. }
  53. }
  54. vector<pair<int,int>> now;
  55. for (auto i : a){
  56. if (mt[i] > i){
  57. now.pb({i, mt[i]});
  58. }
  59. }
  60. if (now.size() > show.size()) show = now;
  61. }
  62.  
  63. void walk(int v){
  64. was[v] = 1;
  65. now.pb(v);
  66. for (auto to : g[v]){
  67. if (!was[to]){
  68. walk(to);
  69. }
  70. }
  71. }
  72.  
  73. int32_t main()
  74. {
  75. #ifdef LOCAL
  76. freopen("input.txt", "r", stdin);
  77. #endif // LOCAL
  78. ios_base::sync_with_stdio(0);
  79. cin.tie(0);
  80. int n, m;
  81. cin >> n >> m;
  82. for (int i = 0; i < m; i++){
  83. int a, b;
  84. cin >> a >> b;
  85. g[a].pb(b);
  86. g[b].pb(a);
  87. }
  88. vector<pair<int,int>> ans;
  89. for (int i = 0; i < n; i++){
  90. if (!was[i]){
  91. now.clear();
  92. walk(i);
  93. for (int i = 0; i < n; i++) solve(now);
  94. for (auto to : show){
  95. ans.pb(to);
  96. }
  97. show.clear();
  98. }
  99. }
  100. cout << ans.size() << endl;
  101. for (auto to : ans){
  102. cout << to.first << ' ' << to.second << endl;
  103. }
  104. return 0;
  105. }
  106.  
  107.  
Advertisement
Add Comment
Please, Sign In to add comment