Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #include "optimization.h"
- #include <unordered_map>
- #include <cassert>
- #include <cstdio>
- #include <algorithm>
- using namespace std;
- int n, e;
- vector<multiset<int>> graph;
- //map<pair<int,int>, int> matrix;
- map<pair<int, int>, int> bad_matrix;
- vector<pair<pair<int, int>, int>> answer;
- set<pair<int,int>> bad;
- void dfs(int v) {
- while (!graph[v].empty()) {
- int x = *graph[v].begin();
- graph[v].erase(graph[v].find(x));
- graph[x].erase(graph[x].find(v));
- dfs(x);
- if (bad.find({x,v}) != bad.end()) {
- bad.erase(bad.find({x,v}));
- bad.erase(bad.find({v,x}));
- answer.push_back({{v, x}, 1});
- }else{
- answer.push_back({{v, x}, 0});
- }
- }
- }
- int main() {
- // cin >> n >> e;
- n = readInt();
- e = readInt();
- graph.resize(n + 1);
- // matrix.resize(n + 1, vector<int8_t>(n + 1, 0));
- // bad_matrix.resize(n + 1, vector<int8_t>(n + 1, 0));
- vector<int> deg(n + 1, 0);
- for (int i = 0; i < e; i++) {
- int a, b;
- // cin >> a >> b;
- a = readInt();
- b = readInt();
- deg[a] += 1;
- deg[b] += 1;
- // matrix[{b,a}] += 1;
- // matrix[{a,b}] += 1;
- graph[a].insert(b);
- graph[b].insert(a);
- }
- vector<int> n_deg;
- for (int i = 1; i <= n; i++) {
- if (deg[i] % 2 == 1) {
- n_deg.push_back(i);
- if (n_deg.size() == 2) {
- int x = n_deg[0];
- int y = n_deg[1];
- bad.insert({x,y});
- bad.insert({y,x});
- graph[x].insert(y);
- graph[y].insert(x);
- n_deg.clear();
- n_deg.resize(0);
- }
- }
- }
- dfs(1);
- vector<vector<int>> result;
- vector<int> way;
- reverse(answer.begin(), answer.end());
- for (auto [x,y] : answer) {
- auto[w, e] = x;
- // cout << w << ' ' << e << '\n';
- if (y == 0) {
- if (way.empty()) {
- way.push_back(w);
- way.push_back(e);
- } else if (way.back() == w) {
- way.push_back(e);
- } else {
- way.push_back(w);
- way.push_back(e);
- }
- } else {
- if (!way.empty()) {
- result.push_back(way);
- }
- way.clear();
- }
- }
- if (!way.empty()) {
- result.push_back(way);
- }
- // int a = prev(result.end())->size();
- int size = result.size();
- int answer_size = size;
- int a = result.size();
- auto last = (prev(result.end()))->size();
- // if (result.begin() != prev(result.end()) && result[0][0] == result[a - 1][last - 1]) {
- // swap(*result.begin(), *prev(result.end()));
- // writeInt(result.size() - 1, '\n');
- // for (int i = 0; i < result.size(); i++) {
- // if (!result[i].empty() && i != result.size() - 1) {
- // for (int j = 0; j < result[i].size(); j++) {
- //// cout << z << ' ';
- // writeInt(result[i][j], ' ');
- // }
- // if (i == 0) {
- // for (int j = 1; j < result[result.size() - 1].size(); j++) {
- // writeInt(result[result.size() - 1][j], ' ');
- // }
- // }
- //// cout << '\n';
- // writeChar('\n');
- // }
- // }
- // return 0;
- // }
- if (size > 1 && result[0][0] == result[size-1][result[size-1].size()-1]){
- answer_size--;
- auto &x = result[0];
- auto &y = result[size-1];
- for (int s = 1; s < x.size(); s++){
- y.push_back(x[s]);
- }
- x.clear();
- }
- // cout << result.size() << '\n';
- writeInt(answer_size, '\n');
- for (auto x: result) {
- if (!x.empty()) {
- for (auto z: x) {
- // cout << z << ' ';
- writeInt(z, ' ');
- }
- // cout << '\n';
- writeChar('\n');
- }
- }
- }
- //5 5
- //1 2
- //1 3
- //1 4
- //1 5
- //2 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement