Advertisement
kolbka_

Untitled

Feb 4th, 2022
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.37 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. #include <iostream>
  8. #include <vector>
  9. #include <string>
  10. #include <stack>
  11. #include <set>
  12. #include <map>
  13.  
  14. #include "optimization.h"
  15. #include <unordered_map>
  16.  
  17. #include <cassert>
  18. #include <cstdio>
  19. #include <algorithm>
  20.  
  21.  
  22. using namespace std;
  23. int n, e;
  24. vector<multiset<int>> graph;
  25. //map<pair<int,int>, int> matrix;
  26. map<pair<int, int>, int> bad_matrix;
  27. vector<pair<pair<int, int>, int>> answer;
  28. set<pair<int,int>> bad;
  29. void dfs(int v) {
  30.     while (!graph[v].empty()) {
  31.         int x = *graph[v].begin();
  32.         graph[v].erase(graph[v].find(x));
  33.         graph[x].erase(graph[x].find(v));
  34.         dfs(x);
  35.         if (bad.find({x,v}) != bad.end()) {
  36.             bad.erase(bad.find({x,v}));
  37.             bad.erase(bad.find({v,x}));
  38.             answer.push_back({{v, x}, 1});
  39.         }else{
  40.             answer.push_back({{v, x}, 0});
  41.  
  42.         }
  43.  
  44.     }
  45.  
  46. }
  47.  
  48.  
  49. int main() {
  50. //    cin >> n >> e;
  51.     n = readInt();
  52.     e = readInt();
  53.     graph.resize(n + 1);
  54. //    matrix.resize(n + 1, vector<int8_t>(n + 1, 0));
  55. //    bad_matrix.resize(n + 1, vector<int8_t>(n + 1, 0));
  56.     vector<int> deg(n + 1, 0);
  57.     for (int i = 0; i < e; i++) {
  58.         int a, b;
  59. //        cin >> a >> b;
  60.         a = readInt();
  61.         b = readInt();
  62.         deg[a] += 1;
  63.         deg[b] += 1;
  64. //        matrix[{b,a}] += 1;
  65. //        matrix[{a,b}] += 1;
  66.         graph[a].insert(b);
  67.         graph[b].insert(a);
  68.     }
  69.     vector<int> n_deg;
  70.  
  71.     for (int i = 1; i <= n; i++) {
  72.         if (deg[i] % 2 == 1) {
  73.             n_deg.push_back(i);
  74.             if (n_deg.size() == 2) {
  75.                 int x = n_deg[0];
  76.                 int y = n_deg[1];
  77.  
  78.                     bad.insert({x,y});
  79.                 bad.insert({y,x});
  80.                     graph[x].insert(y);
  81.                     graph[y].insert(x);
  82.  
  83.                 n_deg.clear();
  84.                 n_deg.resize(0);
  85.             }
  86.         }
  87.     }
  88.  
  89.     dfs(1);
  90.     vector<vector<int>> result;
  91.     vector<int> way;
  92.  
  93.     reverse(answer.begin(), answer.end());
  94.     for (auto [x,y] : answer) {
  95.         auto[w, e] = x;
  96. //        cout << w << ' ' << e << '\n';
  97.         if (y == 0) {
  98.             if (way.empty()) {
  99.                 way.push_back(w);
  100.                 way.push_back(e);
  101.             } else if (way.back() == w) {
  102.                 way.push_back(e);
  103.             } else {
  104.                 way.push_back(w);
  105.                 way.push_back(e);
  106.             }
  107.         } else {
  108.             if (!way.empty()) {
  109.                 result.push_back(way);
  110.             }
  111.             way.clear();
  112.         }
  113.     }
  114.  
  115.     if (!way.empty()) {
  116.         result.push_back(way);
  117.     }
  118. //    int a = prev(result.end())->size();
  119.     int size = result.size();
  120.     int answer_size = size;
  121.     int a = result.size();
  122.     auto last = (prev(result.end()))->size();
  123. //    if (result.begin() != prev(result.end()) && result[0][0] == result[a - 1][last - 1]) {
  124. //        swap(*result.begin(), *prev(result.end()));
  125. //        writeInt(result.size() - 1, '\n');
  126. //        for (int i = 0; i < result.size(); i++) {
  127. //            if (!result[i].empty() && i != result.size() - 1) {
  128. //                for (int j = 0; j < result[i].size(); j++) {
  129. ////                cout << z << ' ';
  130. //                    writeInt(result[i][j], ' ');
  131. //                }
  132. //                if (i == 0) {
  133. //                    for (int j = 1; j < result[result.size() - 1].size(); j++) {
  134. //                        writeInt(result[result.size() - 1][j], ' ');
  135. //                    }
  136. //                }
  137. ////            cout << '\n';
  138. //                writeChar('\n');
  139. //            }
  140. //        }
  141. //        return 0;
  142. //    }
  143.  
  144.  
  145.  
  146.             if (size > 1 && result[0][0] == result[size-1][result[size-1].size()-1]){
  147.                 answer_size--;
  148.                 auto &x = result[0];
  149.                 auto &y = result[size-1];
  150.                 for (int s = 1; s < x.size(); s++){
  151.                     y.push_back(x[s]);
  152.                 }
  153.                 x.clear();
  154.             }
  155.  
  156.  
  157.  
  158.  
  159.  
  160. //    cout << result.size() << '\n';
  161.     writeInt(answer_size, '\n');
  162.     for (auto x: result) {
  163.         if (!x.empty()) {
  164.             for (auto z: x) {
  165. //                cout << z << ' ';
  166.                 writeInt(z, ' ');
  167.             }
  168. //            cout << '\n';
  169.             writeChar('\n');
  170.         }
  171.     }
  172. }
  173.  
  174.  
  175. //5 5
  176. //1 2
  177. //1 3
  178. //1 4
  179. //1 5
  180. //2 3
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement