杭电多校第二场 1003 HDU-6311 Cover(欧拉回路)

Cover
Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 388    Accepted Submission(s): 56
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

 

题目大意:给你一个n个点,m条边的无向图,但这个图不一定联通,现在要问你最少需要画几笔才能将这些边都覆盖,同时要输出边的路径方案。

题目思路:我们先来考虑特殊情况,也就是这个图为欧拉图(也就是图中只有两个度数为奇数的点,或者所有点的度数均为偶数),这样我们只要选择一个度数为奇数的点作为起点跑一遍欧拉回路同时记录路径即可(如果所有点的度数都是偶数的话就选任意一个点就行了)。

再来考虑普遍的情况,如果有多个点的度数为奇数,我们可以将这些点每两个之间加一条虚边,把这个图变成欧拉图,最后再跑一遍欧拉回路,在跑到虚边的时候说明要增加下一笔,再记录一下即可。

举个简单的例子

杭电多校第二场 1003 HDU-6311 Cover(欧拉回路)_第1张图片

对于这个图,一开始有四个度数为奇数的点,我们就可以在1-2之间加上一条虚边(红色的那条边),这样跑一遍欧拉回路得到的结果为 4->2->1->3->2->1->4->3,其中2->1这条边是加入的虚边,拿掉虚边之后最终的结果就是两条路径

4->2->1->3->2

1->4->3

就能得到最后的结果了。

本题有几个坑点,一个是由于图不一定是连通的,所以要考虑孤立点的情况,还有就是最后加虚边的时候要保证还有留下两个度数为奇数的点作为欧拉回路的起点和终点(虽然不知道为什么,但是我一开始是将图加边后连成所有点的度数都是偶数,但跑出来WA了,看了下标程,改成这样才过的,很迷)。

具体实现看代码:

#include 
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lowbit(x) x&-x
#define MP make_pair
#define pb push_back
#define debug(x) cout< vi;
typedef pairpii;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const ll infll=0x3f3f3f3f3f3f3f3f;
const int MX=1e5+7;

int n,m;
struct edge{
    int v,nxt;
}E[MX<<2];
int head[MX],tot,du[MX];
bool vis[MX],vis2[MX<<2];
int num;
vectorv,ans[MX];
void init(){
    clr(head);clr(du);clr(vis);clr(vis2);
    num=0;tot=1;
    v.clear();
}
void add_edge(int u,int v){
    E[++tot].v=v;E[tot].nxt=head[u];
    head[u]=tot;
}
void dfs(int u){
    vis[u]=1;
    if(du[u]&1) v.pb(u);
    for(int i=head[u];i!=0;i=E[i].nxt){
        int v=E[i].v;
        if(vis[v]) continue;
        dfs(v);
    }
}
void dfs2(int u){
    for(int i=head[u];i!=0;i=E[i].nxt){
        if(!vis2[i]){
            vis2[i]=vis2[i^1]=1;
            int v=E[i].v;
            dfs2(v);
            if(i>2*m+1) num++;
            else{
                int cnt=i&1?(i/2):(-i/2);
                ans[num].pb(cnt);
            }
        }
    }
}
void solve(){
    for(int i=1;i<=n;i++){
        if(!vis[i] && du[i]){
            dfs(i);
            for(int j=2;j

 

你可能感兴趣的:(ACM,欧拉回路)