hdu 6311 无向图 欧拉路径

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

Source
2018 Multi-University Training Contest 2

Recommend
chendu | We have carefully selected several similar problems for you: 6437 6436 6435 6434 6433

题意:给出一个图,用最少的欧拉路径去覆盖它,
欧拉回路,对于一个图,如果每个节点的联通度都是偶数,就存在一条欧拉回路。
欧拉路径:对于一个图,如果有k个奇数度的点,那么至少需要k/2条欧拉路径来覆盖所有的边,
可以用dfs来实现寻找欧拉路径,如果当前走到的点有分叉,如果选择某个路由桥,那么它就走不回来了,所有回溯到之前有分叉的节点,往其他路走,
那么可以把走到桥的那条路存在这个点,就可以在下次访问这个点的时候直接告诉它怎么走了,实现的话,用栈实现的,按照出栈的顺序走就好了。

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+100;
int cnt = 0;
vector<int> ans[N*2];
int head[N],to[N*2],nex[N*2],id[N*2];
bool mp[N*3];
int deg[N];
bool vis[N*3];
int tot;
void add_edge(int u,int v,int ID){
    to[++tot] = v,nex[tot] = head[u],id[tot] = ID,head[u] = tot;
    to[++tot] = u,nex[tot] = head[v],id[tot] = -ID,head[v] = tot;
}

void dfs(int x){
    vis[x] = true;
    for(int i = head[x];i;i = nex[i]){
        if(!mp[i]){
            mp[i] = mp[i^1] = true;
            dfs(to[i]);
            //cout << x << ' '<< to[i]<< ' '<< id[i] << ' '<<i << endl;
            if(id[i]) ans[cnt].push_back(-id[i]);
            else ans[++cnt].clear();
        }
    }
}

int main(){
    int n,m;
    while(scanf("%d %d",&n,&m)==2){
        memset(head,0,sizeof(head));
        memset(deg,0,sizeof(deg));
        memset(vis,false,sizeof(vis));
        memset(mp,false,sizeof(mp));
        tot = 1;cnt = 0;
        for(int i = 1;i <= m;i ++){
            int u,v;
            scanf("%d %d",&u,&v);
            add_edge(u,v,i);
            deg[v]++;
            deg[u]++;
        }
        int bef = 0;
        for(int i = 1;i <= n;i ++) {
            if(deg[i]&1) {
                if(bef==0)bef = i;
                else {
                    add_edge(i,bef,0);
                    bef = 0;
                }
            }
        }
        for(int i = 1;i <= n;i ++){
            if(!vis[i]&& deg[i]&1){
                ans[++cnt].clear();
                dfs(i);
                cnt--;
            }
        }
        //cout <<cnt << endl;
        for(int i = 1;i <= n;i ++){
            if(!vis[i]&&deg[i]) {
                ans[++cnt].clear();
                dfs(i);
            }
        }
        printf("%d\n",cnt);
        for(int i= 1;i <= cnt;i ++){
            int m = ans[i].size();
            printf("%d",m);
            for(int j = 0;j < m;j ++){
                printf(" %d",ans[i][j]);
            }
            puts("");
        }
    }


    return 0;
}

猜你喜欢

转载自blog.csdn.net/zstu_zy/article/details/82021437