日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CF528C. Data Center Drama(欧拉回路,构造)

發布時間:2023/12/3 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CF528C. Data Center Drama(欧拉回路,构造) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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}}vk1??et1??vk2??et2??vk3??...vkE+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; }

總結

以上是生活随笔為你收集整理的CF528C. Data Center Drama(欧拉回路,构造)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。