hdu5348 MZL's endless loop(欧拉回路)
生活随笔
收集整理的這篇文章主要介紹了
hdu5348 MZL's endless loop(欧拉回路)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載請注明出處:?http://www.cnblogs.com/fraud/?????????? ——by fraud
?
MZL's endless loop
Time Limit: 3000/1500 MS (Java/Others)????Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1705????Accepted Submission(s): 369
Special Judge
You are given an undirected graph with?n?vertexs and?m?edges. Please direct all the edges so that for every vertex in the graph the inequation?|out?degree???in?degree|≤1?is satisified.
The graph you are given maybe contains self loops or multiple edges.
For each test case, the first line contains two integers?n?and?m.
And the next?m?lines, each line contains two integers?ui?and?vi, which describe an edge of the graph.
T≤100,?1≤n≤105,?1≤m≤3?105,?∑n≤2?105,?∑m≤7?105.
In?ith line contains a integer?1?or?0,?1?for direct the?ith edge to?ui→vi,?0?for?ui←vi.
題意就是給你一張無向圖,讓你把它變成有向圖,使得對于每一個頂點都滿足出度與入度的差的絕對值小于等于一
利用歐拉回路,在歐拉圖中,每個點的出度都等于入度,那么對于這個圖,其實就相當于若干個歐拉圖,然后去掉一些邊。
然后我們需要做的就是補邊。也就是對于每個奇度點,加一條連到其它奇度點的無向邊,然后跑歐拉回路,跑的方向就是這條邊的方向。
另外注意有多個連通分支。這題比較容易T,雖然我的隊友在比賽時瞬間就AC了。。。然而我還是在賽后T了好久,畢竟隊友是final選手
1 /** 2 * code generated by JHelper 3 * More info: https://github.com/AlexeyDmitriev/JHelper 4 * @author xyiyy @https://github.com/xyiyy 5 */ 6 7 #include <iostream> 8 #include <fstream> 9 10 //##################### 11 //Author:fraud 12 //Blog: http://www.cnblogs.com/fraud/ 13 //##################### 14 #pragma comment(linker, "/STACK:102400000,102400000") 15 #include <iostream> 16 #include <sstream> 17 #include <ios> 18 #include <iomanip> 19 #include <functional> 20 #include <algorithm> 21 #include <vector> 22 #include <string> 23 #include <list> 24 #include <queue> 25 #include <deque> 26 #include <stack> 27 #include <set> 28 #include <map> 29 #include <cstdio> 30 #include <cstdlib> 31 #include <cmath> 32 #include <cstring> 33 #include <climits> 34 #include <cctype> 35 36 using namespace std; 37 #define rep(X, N) for(int X=0;X<N;X++) 38 39 const int MAXN = 800010; 40 int head[MAXN]; 41 int Next[MAXN], To[MAXN]; 42 int vis[MAXN]; 43 int used[100010]; 44 int deg[100010]; 45 int gao; 46 int tot; 47 48 void init(int n) { 49 tot = 0; 50 rep(i, n)head[i] = -1; 51 } 52 53 void addedge(int u, int v) { 54 Next[tot] = head[u]; 55 To[tot] = v; 56 vis[tot] = 0; 57 head[u] = tot++; 58 } 59 60 void eular(int u){ 61 used[u] = 1; 62 int i; 63 while(head[u]!=-1){ 64 //for(int &i = head[u];i != -1;i = Next[i]){ 65 i = head[u]; 66 head[u] = Next[head[u]]; 67 if(vis[i])continue; 68 vis[i^1] = 1; 69 eular(To[i]); 70 } 71 } 72 int Scan() { 73 int res=0, ch; 74 while(ch=getchar(), ch<'0'||ch>'9'); 75 res=ch-'0'; 76 while((ch=getchar())>='0'&&ch<='9') 77 res=res*10+ch-'0'; 78 return res; 79 } 80 void Out(int a) { 81 if (a > 9) 82 Out(a / 10); 83 putchar(a % 10 + '0'); 84 } 85 86 class hdu5348 { 87 public: 88 void solve() { 89 int t; 90 t =Scan();//in >> t; 91 while (t--) { 92 int n, m; 93 n = Scan();m=Scan();//in >> n >> m; 94 init(n); 95 rep(i, n)deg[i] = 0; 96 int u, v; 97 rep(i, m) { 98 u = Scan();v= Scan();//in >> u >> v; 99 u--, v--; 100 deg[u]++; 101 deg[v]++; 102 addedge(u, v); 103 addedge(v, u); 104 } 105 gao = -1; 106 rep(i, n) { 107 if (deg[i] & 1) { 108 if (gao != -1) { 109 addedge(i, gao); 110 addedge(gao, i); 111 gao = -1; 112 } else gao = i; 113 } 114 } 115 rep(i, n) used[i] = 0; 116 /*rep(i,n){ 117 if(!used[i]){ 118 dfs(i); 119 num++; 120 } 121 }*/ 122 gao = -1; 123 rep(i, n) { 124 if (!used[i]) { 125 eular(i); 126 } 127 } 128 m<<=1; 129 for(int i=1;i<m;i+=2){ 130 if (vis[i])putchar('1'); 131 else putchar('0'); 132 putchar('\n'); 133 } 134 135 } 136 } 137 }; 138 139 140 int main() { 141 //std::ios::sync_with_stdio(false); 142 //std::cin.tie(0); 143 hdu5348 solver; 144 //std::istream &in(std::cin); 145 //std::ostream &out(std::cout); 146 solver.solve(); 147 return 0; 148 }?
轉載于:https://www.cnblogs.com/fraud/p/4705833.html
總結
以上是生活随笔為你收集整理的hdu5348 MZL's endless loop(欧拉回路)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: You have new mail
- 下一篇: BloomFilter–大规模数据处理利