标签:
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 898 Accepted Submission(s): 178
Special Judge
1 #include<vector> 2 #include<string.h> 3 #include<stdio.h> 4 #pragma comment(linker, "/STACK:1024000000,1024000000") 5 using namespace std; 6 const int M = 3e5 + 10 ; 7 struct Edge { 8 int v ; 9 bool vis ; 10 int nxt ; 11 Edge () {} 12 Edge (int v , int vis , int nxt) : 13 v(v) , vis(vis) , nxt(nxt) {} 14 }e[M << 2] ; 15 int H[M] , E ; 16 17 int n , m ; 18 int in[M] ; 19 int res[M << 1] ; 20 void addedge (int u , int v) { 21 e[E] = Edge (v , 0 , H[u]) ; 22 H[u] = E ++ ; 23 e[E] = Edge (u , 0 , H[v]) ; 24 H[v] = E ++ ; 25 } 26 27 void dfs (int u) { 28 for (int &i = H[u] ; ~i ; ) { 29 int v = e[i].v ; 30 if (e[i].vis) { 31 i = e[i].nxt ; 32 continue ; 33 } 34 e[i].vis = 1 ; 35 e[i^1].vis = 1 ; 36 res[i >> 1] = i & 1 ; 37 in[v] -- ; 38 in[u] -- ; 39 i = e[i].nxt ; 40 dfs (v) ; 41 } 42 } 43 44 void mend () { 45 int p = -1 ; 46 for (int i = 1 ; i <= n ; i ++) { 47 if (in[i] & 1) { 48 if (p == -1) { 49 p = i ; 50 } 51 else { 52 addedge (p , i) ; 53 in[p] ++ ; 54 in[i] ++ ; 55 p = -1; 56 } 57 } 58 } 59 } 60 61 void solve () { 62 mend () ; 63 for (int i = 1 ; i <= n ; i ++) { 64 if(in[i]) { 65 dfs (i) ; 66 } 67 } 68 for (int i = 0 ; i < m ; i ++) printf ("%d\n" , res[i]) ; 69 } 70 71 int main () { 72 int T ; 73 scanf ("%d" , &T ) ; 74 while (T --) { 75 scanf ("%d%d" , &n , &m) ; 76 for (int i = 0 ; i <= n ; i ++) H[i] = -1 ; 77 E = 0 ; 78 for (int i = 0 ; i < m ; i ++) { 79 int u , v ; 80 scanf ("%d%d" , &u , &v) ; 81 addedge (u , v) ; 82 in[u] ++ ; 83 in[v] ++ ; 84 } 85 solve () ; 86 } 87 return 0 ; 88 }
根据“欧拉回路”的定义,当连通图所有点的度数为偶数时,那么必然会存在一条路线,使得经过所有点并且每条边只经过一次
(欧拉通路:除了两个点外度数为奇数,其他点的度数为偶数)
2015多校.MZL's endless loop(欧拉回路的机智应用 || 构造)
标签:
原文地址:http://www.cnblogs.com/get-an-AC-everyday/p/4704017.html