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): 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.
1n,m100000
 

 

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
 

 

Recommend
chendu
 
 
题目大意:给出一张无向图,求出最少几笔可以画完所有的边,并输出路径(反向边输出则要输出 - (边的序号))
 
听了dls的讲解并参考题解得到了一些自己的理解,基本做法就是
1:建图
2:搜索连通块和度为奇数的点
3:在每两个奇数度点内额外加边
4:dfs得到最终结果
 
一张连通图需要n笔画完则有 n = max ( |degree(奇数)| / 2 , 1)
 
 
 
试想每次添加一条边,总度数 +2 ,当有新的笔画出现,则说明出现了一个起点和终点,也就是度为奇数的点。
 
当图上至多只有一对奇数度的点时,便可以一笔走过所有的边
 
删除额外添加的边 (序号>2*m+1)得到结果
 
因为一开始dfs2没有回溯的过程吃了n发WA......
(因为自信的觉得随便选边走一定可以达到目的,但实际上有些边一遍走时过不去的,后来发现要依靠dfs的性质来走完所有边)
 
AC代码(姑且参考了题解):
#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;
        }
    }
}

 

萌新瑟瑟发抖,请求指教。
 

转载于:https://www.cnblogs.com/DevilInChina/p/9370173.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值