CF528C. Data Center Drama
Solution
容易发现,加边后的图必然满足所有点的度为偶数,并且总的边数是偶数,这启发我们使用欧拉回路。
设欧拉回路为vk1et1vk2et2vk3...vk∣E∣+1v_{k_1}e_{t_1}v_{k_2}e_{t_2}v_{k_3}...v_{k_{|E|+1}}vk1et1vk2et2vk3...vk∣E∣+1。我们将et2ie_{t_{2i}}et2i的边定为vk2i→vk2i+1v_{k_{2i}}\to v_{k_{2i+1}}vk2i→vk2i+1的边,et2i−1e_{t_{2i-1}}et2i−1的边定为vk2i→vk2i−1v_{k_{2i}}\to v_{k_{2i-1}}vk2i→vk2i−1的边,这样就能够保证出度入度都是奇数。
现在就是要加最少的边,使得所有点的度为偶数,且边数为偶数。满足前者的边数的下界显然为D2\frac{D}{2}2D(DDD一定是偶数),后者只需要在前者的基础上加一条自环即可。这样加的边数一定达到了下界。
时间复杂度O(n+m)O(n+m)O(n+m)。
Code
#include <bits/stdc++.h>using namespace std;template<typename T> inline bool upmin(T &x, T y) { return y < x ? x = y, 1 : 0; }
template<typename T> inline bool upmax(T &x, T y) { return x < y ? x = y, 1 : 0; }#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se secondtypedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int, int> PR;
typedef vector<int> VI; const lod eps = 1e-9;
const lod pi = acos(-1);
const int oo = 1 << 30;
const ll loo = 1ll << 62;
const int mods = 998244353;
const int MAXN = 400005;
const int INF = 0x3f3f3f3f; //1061109567
/*--------------------------------------------------------------------*/namespace FastIO{constexpr int SIZE = (1 << 21) + 1;int num = 0, f;char ibuf[SIZE], obuf[SIZE], que[65], *iS, *iT, *oS = obuf, *oT = obuf + SIZE - 1, c;#define gc() (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)), (iS == iT ? EOF : *iS ++)) : *iS ++)inline void flush() {fwrite(obuf, 1, oS - obuf, stdout);oS = obuf;}inline void putc(char c) {*oS ++ = c;if (oS == oT) flush();}inline void getc(char &c) {for (c = gc(); (c < 'a' || c > 'z') && c != EOF; c = gc());}template<class I>inline void read(I &x) {for (f = 1, c = gc(); c < '0' || c > '9' ; c = gc()) if (c == '-') f = -1;for (x = 0; c >= '0' && c <= '9' ; c = gc()) x = (x << 3) + (x << 1) + (c & 15);x *= f;}template<class I>inline void print(I x) {if (x < 0) putc('-'), x = -x;if (!x) putc('0');while (x) que[++ num] = x % 10 + 48, x /= 10;while (num) putc(que[num --]);}struct Flusher_{~Flusher_(){flush();}} io_Flusher_;
}
using FastIO :: read;
using FastIO :: print;
using FastIO :: putc;vector<int> V, Ans;
int edgenum = 1, lst, num = 0, d[MAXN], head[MAXN], flag[MAXN << 1];
struct enode{ int to, nxt; } e[MAXN << 1];void add(int u, int v) {e[++ edgenum] = (enode){v, head[u]}, head[u] = edgenum;
}
void dfs(int x) {for (int &i = head[x]; i ; i = e[i].nxt) {if (flag[i]) continue;flag[i] = flag[i ^ 1] = 1;int v = e[i].to;dfs(v);}Ans.PB(x);
}
signed main() {
#ifndef ONLINE_JUDGEfreopen("a.in", "r", stdin);
#endifint n, m; read(n), read(m);for (int i = 1; i <= m ; ++ i) {int u, v; read(u), read(v);add(u, v), add(v, u);++ d[u], ++ d[v];}for (int i = 1; i <= n ; ++ i) if (d[i] & 1) V.PB(i);for (int i = 1; i + i <= (int)V.size() ; ++ i) add(V[i + i - 2], V[i + i - 1]), add(V[i + i - 1], V[i + i - 2]);if (((edgenum ^ 1) >> 1) & 1) add(1, 1), add(1, 1);printf("%d\n", ((edgenum ^ 1) >> 1));dfs(1);for (int i = 0; i < (int)Ans.size() - 1; ++ i)if (i & 1) print(Ans[i]), putc(' '), print(Ans[i + 1]), putc('\n');else print(Ans[i + 1]), putc(' '), print(Ans[i]), putc('\n');return 0;
}