CF527E Data Center Drama
题意
给你一张无向图。
你可以添加若干条边,然后给所有边定向。
使得每一个点的出入度为偶数。
Sol
出入度为偶数,显然为欧拉环路的充要条件。
考虑对于所有原图度数为奇数的点两两相连。
如果不满足边数为偶数直接连自环即可。
跑一边欧拉环路,对于相邻两条边反向连就行了。
Code
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <vector>
#include <bitset>
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2= buf, ubuf[1 << 23], *u = ubuf;
#endif
int read() {
int p = 0, flg = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') flg = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
p = p * 10 + c - '0';
c = getchar();
}
return p * flg;
}
void write(int x) {
if (x < 0) {
x = -x;
putchar('-');
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
const int N = 2e5 + 5, M = 8e5 + 5;
array <int, N> deg;
namespace G {
array <int, N> fir;
array <int, M> nex, to;
int cnt = 1;
void add(int x, int y) {
cnt++;
nex[cnt] = fir[x];
to[cnt] = y;
fir[x] = cnt;
}
void _add(int x, int y) {
deg[x]++, deg[y]++;
add(x, y), add(y, x);
}
}
bitset <M> vis;
vector <int> isl;
int op = 1;
void dfs(int x) {
for (int &i = G::fir[x]; i; i = G::nex[i]) {
if (vis[i]) continue;
vis[i] = vis[i ^ 1] = 1;
int _x = x, _y = G::to[i];
dfs(G::to[i]);
if (op & 1) swap(_x, _y);
write(_x), putchar(32);
write(_y), puts("");
op ^= 1;
}
}
int main() {
int n = read(), m = read();
for (int i = 1; i <= m; i++)
G::_add(read(), read());
for (int i = 1; i <= n; i++) {
if (!(deg[i] & 1)) continue;
isl.push_back(i);
}
for (int i = 0; i < (int)isl.size(); i += 2)
m++, G::_add(isl[i], isl[i + 1]);
if (m & 1) m++, G::_add(1, 1);
write(m), puts("");
dfs(1);
return 0;
}