杭电多校第二场 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条边的无向图,但这个图不一定联通,现在要问你最少需要画几笔才能将这些边都覆盖,同时要输出边的路径方案。

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

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

举个简单的例子

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

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

1->4->3

就能得到最后的结果了。

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

具体实现看代码:

#include <bits/stdc++.h>
#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<<x<<"= "<<x<<endl
#define FIN freopen("in.txt","r",stdin)
#define clr(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int>pii;
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;
vector<int>v,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<v.size();j+=2){
                add_edge(v[j],v[j+1]);
                add_edge(v[j+1],v[j]);
            }
            num++;
            if(v.size()) dfs2(v[0]);
            else dfs2(i);
            v.clear();
        }
    }
    printf("%d\n",num);
    for(int i=1;i<=num;i++){
        int sz=ans[i].size();
        printf("%d ",sz);
        for(int j=0;j<sz;j++)
            printf("%d%c",ans[i][j],j==sz-1?'\n':' ');
        ans[i].clear();
    }
}

int main(){
    while(~scanf("%d%d",&n,&m)){
        init();
        for(int i=1;i<=m;i++){
            int u,v;scanf("%d%d",&u,&v);
            add_edge(u,v);add_edge(v,u);
            du[u]++;du[v]++;
        }
        solve();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lee_w_j__/article/details/81214758