zoukankan      html  css  js  c++  java
  • 2015多校.MZL's endless loop(欧拉回路的机智应用 || 构造)

    MZL's endless loop

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 898    Accepted Submission(s): 178
    Special Judge


    Problem Description
    As we all kown, MZL hates the endless loop deeply, and he commands you to solve this problem to end the loop.
    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.
     
    Input
    The first line of the input is a single integer T, indicating the number of testcases.
    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.
    T1001n1051m3105n2105m7105.
     
    Output
    For each test case, if there is no solution, print a single line with 1, otherwise output m lines,.
    In ith line contains a integer 1 or 01 for direct the ith edge to uivi0 for uivi.
     
    Sample Input
    2 3 3 1 2 2 3 3 1 7 6 1 2 1 3 1 4 1 5 1 6 1 7
     
    Sample Output
    1 1 1 0 1 0 1 0 1
     
     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
    " , 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 }
    View Code

    根据“欧拉回路”的定义,当连通图所有点的度数为偶数时,那么必然会存在一条路线,使得经过所有点并且每条边只经过一次

    所以很明显如果我们能在构造是利用好这个性质的话,整个复杂度为O(m + k)
    为什么还有一个常数k?你很容易回发现,题目给定的边数不一定回使每个点的度数为 偶数 , 那么怎么办呢?补边咯,把两两为奇数度的点之间加一条边即可。
    那么你可定又会有疑问了,这样添加边会不会导致最后的 “题设的条件” 收到影响?
    没事的,因为题目说了 |入度 - 出度| <= 1 ,因为你构造的是欧拉回路,所以找到后的欧拉回路肯定满足所有点|入度 - 出度| = 0 , 而我们在每个点上最多只加了
    一条边,所以去掉后,肯定 <= 1 的。
     
    铭神说,构造回路时,因为每个点可能会被遍历到多次,这样如果姿势不对,很容易导致又把那个点的所有边遍历一遍,导致复杂度又变成O(n*m),所以去仔细
    看代码吧233
     

     (欧拉通路:除了两个点外度数为奇数,其他点的度数为偶数)

  • 相关阅读:
    1.saltstack入门
    构造函数
    gitlab
    mysql常见的问题
    python数据运算
    BZOJ 1691 usaco2007 挑剔的美食家
    BZOJ 1708 usaco2007 Money奶牛的硬币
    BZOJ 1050 HAOI2006 旅行comf
    BZOJ 2427 HAOI2010 软件安装
    BZOJ 1901 Dynamic Rankings
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4704017.html
Copyright © 2011-2022 走看看