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. pspad的一个怪现象:在一些空行的位置出现个别不该出现的字符

    在用pspad编辑一个外来文件时,发现有许多空行的结尾会出现一些单个字符,字符内容与翻页前那一页相应位置的字符相同. 好奇怪.上网找不到原因.pspad太好用了,不想因此放弃. 仔细观察,这些空行往往 ...

  2. HALCON算子1

    https://blog.csdn.net/think_fast/article/details/7011364 待验证学习

  3. CocosCreator工程内的命名

    命名结构总体的命名结构遵循以下格式 前缀-内容-尾缀 - 前缀:用来定义node的属性- 内容:node的名字- 尾缀:序列或状态1231. 前缀说明:在开始的时候定义/声明这个节点的属性前缀可以是一 ...

  4. oracle插入中文乱码

    执行sql: select  userenv('language')     from dual;  --  AMERICAN_AMERICA.ZHS16GBK select * from v$nls ...

  5. regular expression matching DP

    这个题目,我从前天晚上(8月6号晚上)调试到现在(8月8号16:21),太心酸了,不好好总结一下,就太对不起自己了! 这是题目: Implement regular expression matchi ...

  6. Caused by: java.lang.IllegalArgumentException: Parameter Maps collection does not contain value for com.bj186.crm.mapper.UserMapper.Integer

    在使用SSM整合myBatis的过程中遇到了这个问题. 问题的原因: 把parameterType错误的写成了parameterMap 解决办法: 将parameterMap修改为parameterT ...

  7. HTML基础(一)

    什么是HTMLHTML Hypertext Markup Language:即超文本标记语言 HTML特点 1.HTML不需要编译,直接由浏览器执行 2.HTML文件是一个文本文件 3.HTML文件必 ...

  8. Perl语言入门: 斜线不是元字符,所以在不作为分隔符时不需要加上反斜线。

    Perl语言入门: 斜线不是元字符,所以在不作为分隔符时不需要加上反斜线.

  9. mybatis-使用junit测试与main方法测试结果不一致问题

    今天使用ieda写mybatis程序感觉太不友好了,而且也没找到问题所在.问题:写的user.xml中的语句与输出的语句不一样.尝试了各种办法都没有解决. mybatis配置的使用的查询实体.xml ...

  10. struts2中关于jsp页面向action传值出现乱码问题

    在JSP页面输入中文并传给后台的时候,常常会出现乱码问题,产生乱码的原因:java在进行传值的时候,默认用的是iso-8859-1的编码形式进行传输,而我们jsp页面常用的则是utf-8的编码形式.所 ...