hdu 6311 无向图 欧拉路径

本文介绍了一种算法,用于解决如何用最少的欧拉路径覆盖一个图的问题。通过分析图中节点的度数,确定所需的最少路径数量,并利用深度优先搜索(DFS)策略来寻找这些路径。特别关注于奇数度节点的处理,通过引入虚拟边来确保图的连通性和欧拉路径的存在。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值