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. COGS 1570. [POJ3461]乌力波

    ★☆   输入文件:oulipo.in   输出文件:oulipo.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 法国作家乔治·佩雷克(Georges Perec,1 ...

  2. MFC技术积累——基于MFC对话框类的那些事儿4

    3.3.4 借助兼容DC加载DIB位图 创建一个与设备环境相兼容的DC,通过将位图暂时导入至兼容DC,然后利用CDC::BitBlt 或者CDC::StretchBlt函数将位图绘制到设备环境中. 示 ...

  3. vue 高度 动态更新计算 calcHeight watch $route

    vue 高度 动态更新计算 calcHeight () { // this.tableHeight = window.innerHeight - 210 } }, mounted () { // co ...

  4. ADB相关指令实例详解

    1.获取物理设备相关信息: adb devices 2.apk安装 2.1 新的应用(该应用未安装) adb -s 设备序列号 install apk路径 2.2 应用已安装,重安装覆盖 adb -s ...

  5. 公共dao的抽取

    package cn.sxx.dao; import java.util.List; import cn.sxx.model.Dep; import cn.sxx.query.DepQuery; pu ...

  6. TWaver3D直线、曲线、曲面的绘制

    插播一则广告(长期有效) TWaver需要在武汉招JavaScript工程师若干 要求:对前端技术(JavasScript.HTML.CSS),对可视化技术(Canvas.WebGL)有浓厚的兴趣 基 ...

  7. ios字体简单设定

    UILabel *lable = [[UILabel alloc] init]; label.font = [];

  8. Puppeteer-常规操作一

    这里不讲 Puppeteer 怎么使用,主要讲一些常规操作在这里如何通过另类方法实现.等实现后,你就会感觉,嗯~~ 真香! 场景一 已经找出要的元素,现在有需求再继续寻找他的子元素 第一种.将父元素带 ...

  9. alt、title和label

    alt是html标签的属性,而title既是html标签,又是html属性. title标签这个不用多说,网页的标题就是写在<title></title>这对标签之内的. ti ...

  10. Android Studio修改默认字体大小

    安装Android Studio后,默认的字体很小,看着很不舒服,如下图 因此,我们需要改变字体大小,步骤如下: 一.打开设置 二.找到Font,这里系统的主题不能修改,我们点击Save As... ...