Cover

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

解析  对于每个连通块最少路径覆盖就是  max(1,度数为奇数点的个数/2)  然后我们把每两个奇数点连一条虚边 要剩两个 我们求欧拉路径或者欧拉回路 都可以求解  求得的路径删掉虚边 剩下的线段就是最少的路径  但是 求欧拉路径比较容易计算路径条数 所以剩两个从一个奇数点出发回到另一个奇数点   (没有奇数点就从任意一点出发 得到欧拉回路 没有虚边 一条路就可以走完 ) 独立点不要算进来 覆盖所有的边 不是所有的点。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

欧拉回路 或者 欧拉路径都可以直接dfs得到  回朔的时候保存路径就好了 因为走死一个圈 退回去 转一圈还可以回来

欧拉回路求解算法:

https://blog.csdn.net/u011466175/article/details/18861415

AC代码

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+,mod=1e9+;
const ll inf=1e16;
#define pb push_back
#define mp make_pair
struct edge
{
int from,to;
edge(int u,int v):from(u),to(v) {}
};
int n,m,cnt;
vector<int> ans[maxn],ji;
vector<edge> edges;
vector<int> g[maxn];
int du[maxn];
int p[maxn*],vis[maxn];//p标记边是否用过要开大一点 因为自己还要加边,vis标记点是否访问过
void init()
{
cnt=;
for(int i=; i<=n; i++) g[i].clear(),ans[i].clear();
memset(vis,,sizeof(vis));
memset(du,,sizeof(du));
memset(p,,sizeof(p));
edges.clear();
}
void addedge(int from,int to)
{
edges.push_back(edge(from,to));
edges.push_back(edge(to,from));
int siz=edges.size();
g[from].push_back(siz-);
g[to].push_back(siz-);
}
void dfs(int x)
{
vis[x]=;
if(du[x]%)
ji.pb(x);
for(int i=;i<g[x].size();i++)
{
edge e=edges[g[x][i]];
if(!vis[e.to])
dfs(e.to);
}
}
void dfs2(int x)
{
for(int i=;i<g[x].size();i++)
{
int u=g[x][i];
edge e=edges[u];
if(!p[u])
{
p[u]=p[u^]=;
dfs2(e.to);
int temp=u%?-u/-:u/+;
if(u<*m)
ans[cnt].pb(temp);
else
cnt++;
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
int u,v;init();
for(int i=;i<=m;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
du[u]++,du[v]++;
}
for(int i=;i<=n;i++)
{
if(!vis[i]&&du[i])
{
ji.clear();
dfs(i);
for(int i=;i<ji.size();i+=)
{
addedge(ji[i],ji[i+]);
}
int t=ji.size()?ji[]:i;
dfs2(t);
cnt++;
}
}
printf("%d\n",cnt);
for(int i=;i<cnt;i++)
{
printf("%d",ans[i].size());
for(int j=ans[i].size()-;j>=;j--)
{
printf(" %d",ans[i][j]);
}
printf("\n");
}
}
}

HDU 6311 最少路径覆盖边集 欧拉路径的更多相关文章

  1. poj 1422 Air Raid 最少路径覆盖

    题目链接:http://poj.org/problem?id=1422 Consider a town where all the streets are one-way and each stree ...

  2. hdu 1151 最小路径覆盖

    先说说最小路径覆盖的定义 定义:在一个有向图中,找出最少的路径,使得这些路径,经过每一个点,且每一个点只与一条路径相关联, 由上面得出: 1.一个单独的点是一个路径 2:如果有路径a,b,c....f ...

  3. Antenna Placement(匈牙利算法 ,最少路径覆盖)

    Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6991   Accepted: 3466 ...

  4. Hdu 1068 最小路径覆盖

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. HDU - 6311 Cover(无向图的最少路径边覆盖 欧拉路径)

    题意 给个无向图,无重边和自环,问最少需要多少路径把边覆盖了.并输出相应路径 分析 首先联通块之间是独立的,对于一个联通块内,最少路径覆盖就是  max(1,度数为奇数点的个数/2).然后就是求欧拉路 ...

  6. HDU 6311 Cover (无向图最小路径覆盖)

    HDU 6311 Cover (无向图最小路径覆盖) Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...

  7. (hdu step 6.3.3)Air Raid(最小路径覆盖:求用最少边把全部的顶点都覆盖)

    题目: Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  8. hdu 1151 Air Raid(二分图最小路径覆盖)

    http://acm.hdu.edu.cn/showproblem.php?pid=1151 Air Raid Time Limit: 1000MS   Memory Limit: 10000K To ...

  9. HDU 3861 The King’s Problem 最小路径覆盖(强连通分量缩点+二分图最大匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 最小路径覆盖的一篇博客:https://blog.csdn.net/qq_39627843/ar ...

随机推荐

  1. C# string日期格式

    百分数格式应该用“p”这个参数. 格式 原始 数据 结 果 "{0:P}" 0.40 40% 数字 {0:N2} 12.36  数字 {0:N0} 13  货币 {0:c2} $1 ...

  2. Servlet 3.0 新特性详解 (转载)

    原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-servlet30/ Servlet 3.0 新特性概述 Servlet 3.0 作为 Jav ...

  3. XtraBackUp 热备份工具

    是一款强大的在线热备份工具 备份的过程中,不锁表 使用percona-xtrabackup-24-2.4.7-1.el7.x86_64.rpm yum源安装: 1.安装Percona的库:       ...

  4. how to make a function from using global var to not using it

    let say, we want to find the bottom left node in a tree.one way to do it is using global vars: /** * ...

  5. Open Cascade:使用鼠标画线

    Open Cascade:使用鼠标画线 在View类文件中创建以下代码: 1.创建鼠标消息: afx_msg void OnLButtonDown(UINT nFlags, CPoint point) ...

  6. mysql中ibatis的limit动态传参

    param.put("pageNo",pageNo);   param.put("pageSize",pageSize); sqlMap中的用法 limit $ ...

  7. Redis主从配置与数据备份还原

    一.主从配置: 1.下载: wget http://download.redis.io/releases/redis-4.0.9.tar.gz tar xzf redis-4.0.9.tar.gz c ...

  8. 条款16:成对使用new和delete时要采取相同形式

    NOTE: 1.如果你在new表达式中使用[],必须在相应的delete表达式中也使用[].如果你在new表达式中不使用[],一定不要在相应的delete表达式中使用[].

  9. Tiny4412 U-BOOT移植(转)

    http://blog.csdn.net/eshing/article/details/37520291(转) 一.移植前说明: 1.  特别声明:此文档是我的学习文档,里面肯定有错误地方,仅供参考! ...

  10. jquery validate基本

    http://www.runoob.com/jquery/jquery-plugin-validate.html jquery validate 默认 在键盘按下并释放及提交后验证提交表单 例如: $ ...