Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <vector>
- #include <iostream>
- #include <fstream>
- #include <iostream>
- #include <utility>
- #include <unordered_map>
- #include <stack>
- #include <string>
- #include <map>
- #include <cstring>
- #include <algorithm>
- #include <set>
- #include <cmath>
- #include <ctime>
- #include <queue>
- #include <chrono>
- #include <bitset>
- #include <unordered_set>
- #include <assert.h>
- #include <sstream>
- #include "optimization.h"
- using namespace std;
- typedef long long LL;
- #define INF 90000000
- #include <cstdio>
- #include <cassert>
- #include <cstdio>
- #include <algorithm>
- /** Fast allocation /
- const int MAX_MEM = 1e8;
- int mpos = 0;
- char mem[MAX_MEM];
- inline void * operator new (size_t n) {
- assert((mpos += n) <= MAX_MEM);
- return (void *)(mem + mpos - n);
- }
- inline void operator delete (void *) noexcept { } // must have!
- /**/
- #define MAX_N 20000 + 1
- using namespace std;
- static int n, m, ans, color = 1;
- stringstream SS;
- struct trin
- {
- int id, b, e;
- trin() {};
- trin(int a, int b, int c) : id{ a }, b{ b }, e{ c } {}
- };
- bool operator<(const trin &t1, const trin &t2)
- {
- if (t1.b == t2.b)
- if (t1.e == t2.e)
- return t1.id < t2.id;
- else
- return t1.e < t2.e;
- return t1.b < t2.b;
- }
- typedef pair<int, int> pair_t;
- set<pair_t> FE[MAX_N];
- vector<vector<int>> Components, Paths;
- vector<int> Component;
- static bool marked[MAX_N];
- static int Cnt[MAX_N];
- //static int Went[MAX_N * 3 + 10];
- unordered_map<int, int> Went;
- int id = 0;
- stack<int> St;
- int dfs_e(int v, int neg)
- {
- Cnt[v]++;
- for (auto e : FE[v])
- {
- if (!Went[e.first])
- {
- Went[e.first] = 1;
- dfs_e(e.second, e.first >= m);
- }
- }
- St.push(v);
- if (neg)
- St.push(-1);
- return 0;
- }
- int solve_c(vector<int> &C)
- {
- if (C.size() == 2 || C.size() == 1)
- {
- Paths.push_back(C);
- return 1;
- }
- int start = C[0], cnt = 0;
- for (int i = 0; i < C.size(); ++i)
- {
- int c_i = C[i];
- if (FE[c_i].size() % 2)
- {
- for (int j = i + 1; j < C.size(); ++j)
- {
- int c_j = C[j];
- if (FE[c_j].size() % 2)
- {
- FE[c_i].insert({ id, c_j });
- FE[c_j].insert({ id, c_i });
- start = c_i;
- cnt++;
- id++;
- break;
- }
- }
- start = c_i;
- }
- }
- vector<int> Path;
- if (cnt == 0)
- cnt++;
- else if (FE[start].size() % 2)
- cnt++;
- dfs_e(start, 0);
- int next = St.top();
- St.pop();
- if (St.top() == -1)
- St.pop();
- else
- Path.push_back(next);
- int p = next;
- while (St.size())
- {
- if (St.top() == -1)
- {
- Paths.push_back(Path);
- Path.clear();
- }
- else if (p != -1 || St.size() > 1)
- Path.push_back(St.top());
- p = St.top();
- St.pop();
- }
- if (Path.size())
- Paths.push_back(Path);
- return cnt;
- }
- int main(void)
- {
- n = readInt();
- m = readInt();
- id = m;
- for (int i = 0; i < m; ++i)
- {
- int a, b;
- a = readInt();
- b = readInt();
- FE[a].insert({ i, b });
- FE[b].insert({ i, a });
- }
- for (int i = 1; i <= n; ++i)
- Component.push_back(i);
- int res = 0;
- res += solve_c(Component);
- writeInt(res);
- writeChar('\n');
- for (auto &p : Paths)
- {
- for (auto v : p)
- writeInt(v), writeChar(' ');
- writeChar('\n');
- }
- flush();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement