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\n",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.
1≤n,m≤100000

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:

原文地址:https://www.cnblogs.com/zhangbuang/p/11700769.html

时间: 2024-08-05 23:02:11

hdu 6311 Cover (欧拉路径)的相关文章

HDU - 6311 Cover(无向图的最少路径边覆盖 欧拉路径)

题意 给个无向图,无重边和自环,问最少需要多少路径把边覆盖了.并输出相应路径 分析 首先联通块之间是独立的,对于一个联通块内,最少路径覆盖就是  max(1,度数为奇数点的个数/2).然后就是求欧拉路径了,先将块内度数为奇数的点找出来,留下两个点,其余两两连上虚边,这样我们选择从一个奇数点出发到另一个奇数点,求出一条欧拉路径,统计总路径数.接着就dfs,注意一些细节. 附赠一个求欧拉回路的fleury算法:https://blog.csdn.net/u011466175/article/deta

HDU 6311 Cover

Cover Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 440    Accepted Submission(s): 65Special Judge Problem Description The Wall has down and the King in the north has to send his soldiers to s

HDU 5386 Cover(模拟)

Cover Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 966    Accepted Submission(s): 320 Special Judge Problem Description You have an n?n matrix.Every grid has a color.Now there are two types

HDU 5386 Cover

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5386 题目大意:给一个初始矩阵(n×n).一个目标矩阵(n×n)和m个操作,要求找到一种操作顺序,使初始矩阵变成目标矩阵.操作共有两种,如下: L x y: 把当前矩阵第x列的数全变为y H x y: 把当前矩阵第x行的数全变为y 输入格式:先输入case数T,每个case第一行是两个整数n和m,接下来n行输入初始矩阵,再下来n行输入目标矩阵.最后m行输入操作. 1≤color[i][j]≤n,co

hdu 5386 Cover(暴力求解+想法题)

题意: 有两种操作: 操作L x y,把当前x,这一列全部置为y 操作H x y,把当前,这一行全部置为y. 现在给你n?n的初始矩阵,以及n?n的目标矩阵 现在给你m种操作(由以上两种操作构成),问怎么排序这m种操作,才能使得,初始矩阵,经由排序后的操作,构成目标矩阵. 输出排序方案. 解析: 逆向思维, 枚举每个操作,然后判断该操作是不是最后一个操作.(就像撕胶布一样,一条一条的剥离) 判断是否是最后一个操作方法就是: 除去已经用过的点,如果一排都等于当前操作的颜色,那就是最后一个操作.然后

HDU 5386 Cover (2015年多校比赛第8场)

1.题目描述:点击打开链接 2.解题思路:本题利用逆向思维+贪心法解决.因为题目中已经告诉我们一定存在解,因此可以考虑贪心法的使用.这道题的妙处在于答案和初始矩阵是无关的,只和目标矩阵有关.因为不管初始矩阵长什么样,只要操作一样,加上解的存在性,得到的目标矩阵一定是相同的.接下来就是如何寻找操作序列. 假设最后一步操作执行后,我们得到了目标矩阵,由于所有操作都是对一整行或者一整列进行的,因此肯定有一行或者一列全部相同.这样,我们就可以从目标矩阵出发,逆着这个过程寻找,如果发现某一行或者一列全部相

HDU 5386 Cover (MUT #8 模拟暴力)

[题目链接]:click here~~ [题意]: 操作L x y,把当前x,这一列全部置为y 操作H x y,把当前,这一行全部置为y. 现在给你n?n的初始矩阵,以及n?n的目标矩阵 现在给你m种操作(由以上两种操作构成),问怎么排序这m种操作,才能使得,初始矩阵,经由排序后的操作,构成目标矩阵. 输出排序方案. 也就是给出初始矩阵和目标矩阵,存在m中操作,可以分别把每行或者每列都涂成同一种颜色,数据保证有解,因为保证有解,(然而初始矩阵并没有什么卵用...) [思路]: 暴力寻找M次操作,

HDU 6311 Harvest of Apples (组合数,莫队)

场上怎么都想不出来,看了标程想自闭... #include <algorithm> #include <iostream> #include <cstdio> #include <vector> #include <cmath> using namespace std; #define N 100005 #define mod 1000000007 struct query{ int n,k,i; }Q[N]; bool cmp(const qu

hdu 4850 Wow! Such String! 构造 或 欧拉路径并改写成非递归版本

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4850 跟这道题也算是苦大仇深了... 题意:构造一个由26个小写字母组成的.无长度为4的重复子串的字符串(要求出能构造出的最大长度) 符合要求的长度为4的字符串有4^26个 容易猜最大长度是:4^26+3 = 456979 比赛的时候想法是要让各个字母出现得尽量“均匀” 用了一个cnt数组记录26个字母出现的次数 每次都选择出现次数最小的.不与前面重复的字母加上去 然而稍微写得有点歪,最后构造出了长