zoukankan      html  css  js  c++  java
  • hdu 6311 Cover (欧拉路径)

    #include<bits/stdc++.h>
    #define N 100005
    using namespace std;
    struct Edge{
        int to,next;
        bool able;
    }edge[N*4];///要多于总边数的4倍 (*2双向边 并且可能加边)
    
    int n,m,
    Degree[N],///每个点的度
    Head[N],  ///每个点的最后一条加入的边的序号
    cnt,      ///边的序号
    res;      ///一共找到的路径
    
    bool vis[N];
    vector<int>st;///保存一个连通块中度为奇数的点
    vector<int>road[N];
    
    void add(int u,int v){
        edge[++cnt].next = Head[u];
        edge[cnt].to = v;
        edge[cnt].able = true;
        Head[u] = cnt;
        ++Degree[u];
    }
    inline void add_edge(int u,int v){
        add(u,v);
        add(v,u);
    }
    
    void dfs(int s){
        vis[s] = true;
        if(Degree[s]&1)st.push_back(s);
        for(int i = Head[s] ; i ; i = edge[i].next){
            if(!vis[edge[i].to])dfs(edge[i].to);
        }
    }
    
    void dfs2(int s){
        for(int i = Head[s] ; i ; i = edge[i].next){
            if(edge[i].able) {
                edge[i].able = edge[i ^ 1].able = false;
                dfs2(edge[i].to);
                if(i>2*m+1)++res;///说明此边是由奇数度点添加得到的,所以这条回路已经结束
                else {
                    road[res].push_back(i/2*(2*(i&1)-1));
                }
            }
        }
    }
    
    int main(){
        int u,v;
        while (cin>>n>>m){
            cnt = 1,res = 0;
            for(int i = 0 ; i < m ; ++i){
                scanf("%d %d",&u,&v);
                add_edge(u,v);
            }
            for(int i = 1 ; i <= n ; ++i){
                if(!vis[i] and Degree[i]) {
                    dfs(i);///找到连通块和奇数度的点
                    if (st.empty()) {
                        st.push_back(i);
                        st.push_back(i);
                    }
                    for (int j = 2; j < st.size(); j += 2) {///为从第二对开始的奇数度的点添加一条双向边
                        add_edge(st[j], st[j + 1]);
                    }
                    res++;
                    dfs2(st[0]);
                    st.clear();
                }
            }
            
            printf("%d
    ",res);
            for(int i = 1 ; i <= res ; ++i){
                printf("%d",road[i].size());
                for(int j = 0 ; j < road[i].size() ; ++j){
                    printf(" %d",road[i][j]);
                }
                puts("");
                road[i].clear();
            }
            for(int i = 1 ; i <= n ; ++i){
                vis[i] = false;
                Head[i] = 0;
                Degree[i] = 0;
            }
        }
    }
    

      

    Cover

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 440    Accepted Submission(s): 65
    Special Judge


    Problem Description
    The Wall has down and the King in the north has to send his soldiers to sentinel.
    The North can be regard as a undirected graph (not necessary to be connected), one soldier can cover one path. Today there's no so many people still breathing in the north, so the King wants to minimize the number of soldiers he sent to cover each edge exactly once. As a master of his, you should tell him how to arrange soldiers.
     
    Input
    There might be multiple test cases, no more than 20. You need to read till the end of input.
    In the first line, two integers n and m, representing the number of nodes and edges in the graph.
    In the following m lines, each contain two integers, representing two ends of an edge.
    There are no parallel edges or self loops.
    1n,m100000
     
    Output
    For each test case, the first line contains number of needed routes, p.
    For the following p lines, an integer x in the beginning, followed by x integers, representing the list of used edges. Every integer should be a positive or negative integer. Its absolute value represents the number of chosen edge (1~n). If it's positive, it shows that this edge should be passed as the direction as the input, otherwise this edge should be passed in the direction different from the input. Edges should be in correct order.
     
    Sample Input
    3 3
    1 2
    1 3
    2 3
     
    Sample Output
    1
    3 1 3 -2
     

     Solution:

    在每一个联通块内考虑,设联通块内,一张连通图需要n笔画完则有 n = max ( |degree(奇数)| / 2 , 1)

    如果有两个奇度顶点,其他的都为偶数,那么也可以选择任意的一个奇度顶点来跑欧拉路径。但是如果一个联通块的奇度顶点大于2,那么就需要对奇度顶点之间进行连边,然后选择一个奇度顶点跑欧拉路径,然后最后将多连的边删去就好了

    Code:

  • 相关阅读:
    MYSQL中replace into的用法以及与inset into的区别
    怎么安装phpcms?PHPCMS V9安装图文教程
    Yii 框架生成缩略图
    怎么让普通用户使用root权限执行用户命令
    自学Linux命令的四种方法
    最完整PHP.INI中文版
    前端chrome浏览器调试
    phpstorm快捷键记录
    客户关系管理
    Subquery returns more than 1 row
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/11700769.html
Copyright © 2011-2022 走看看