CF528C. Data Center Drama(欧拉回路,构造)
生活随笔
收集整理的這篇文章主要介紹了
CF528C. Data Center Drama(欧拉回路,构造)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
CF528C. Data Center Drama
Solution
容易發(fā)現(xiàn),加邊后的圖必然滿足所有點的度為偶數(shù),并且總的邊數(shù)是偶數(shù),這啟發(fā)我們使用歐拉回路。
設(shè)歐拉回路為vk1et1vk2et2vk3...vk∣E∣+1v_{k_1}e_{t_1}v_{k_2}e_{t_2}v_{k_3}...v_{k_{|E|+1}}vk1??et1??vk2??et2??vk3??...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??的邊,這樣就能夠保證出度入度都是奇數(shù)。
現(xiàn)在就是要加最少的邊,使得所有點的度為偶數(shù),且邊數(shù)為偶數(shù)。滿足前者的邊數(shù)的下界顯然為D2\frac{D}{2}2D?(DDD一定是偶數(shù)),后者只需要在前者的基礎(chǔ)上加一條自環(huán)即可。這樣加的邊數(shù)一定達到了下界。
時間復(fù)雜度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; }總結(jié)
以上是生活随笔為你收集整理的CF528C. Data Center Drama(欧拉回路,构造)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 心脏支架和搭桥有什么区别
- 下一篇: 雪诺酮是什么