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. 介绍三款大前端UI框架

    一.蚂蚁金服团队推出的基于React antd (全名:ant.design) 友情跳链:https://ant.design/index-cn:使用antd模板:https://pro.ant.de ...

  2. SDUT_2146:最小子序列和

    题目描述 给你一个长为n(10<=n<=10000)的数组,数组中的每一个数大于等于1小于等于1000000.请你找出一个长为k(1<=k<=1000)的子序列.找序列时,假如 ...

  3. Android(java)学习笔记169:服务(service)之为什么使用服务

    1.服务 service 长期在后台运行的进程,一般没有应用程序界面   2.进程线程和应用程序之间的关系 应用程序开启,系统启动一个Linux进程,所有的组件都是运行在同一个进程的同一个线程(mai ...

  4. Elasticsearch学习(二)————搜索

    Elasticsearch1.query string search1.1.搜索全部// 1. GET http://ip:9200/test/test/_search 结果: { "too ...

  5. vue $parent 的上一级 有可能不是父组件,需要好几层$parent 如果这样 还不如用 this.$emit

    vue $parent 的上一级 有可能不是父组件,需要好几层$parent 如果这样 还不如用 this.$emit

  6. Java数据结构和算法(三)--三大排序--冒泡、选择、插入排序

    三大排序在我们刚开始学习编程的时候就接触过,也是刚开始工作笔试会遇到的,后续也会学习希尔.快速排序,这里顺便复习一下 冒泡排序: 步骤: 1.从首位开始,比较首位和右边的索引 2.如果当前位置比右边的 ...

  7. 项目中常用的js方法(持续更新)

    <script> var utils = { //时间戳转日期(timestamp:时间戳 默认当前时间) dateFormat: function(timestamp = new Dat ...

  8. List去重复数据

    for ( int i = 0 ; i < list.size() - 1 ; i ++ ) {  for ( int j = list.size() - 1 ; j > i; j -- ...

  9. node程序的部署神器pm2的基本使用

    pm2是从nodejs衍生出来的服务器进程管理工具,可以做到开机就启动nodejs.当然了,也可以用nohup来做这件事情的. 前言 众所周知,Node.js运行在Chrome的JavaScript运 ...

  10. ios 自定义URL Scheme 设计

    在 iOS 里,程序之间都是相互隔离,目前并没有一个有效的方式来做程序间通信,幸好 iOS 程序可以很方便的注册自己的 URL Scheme,这样就可以通过打开特定 URL 的方式来传递参数给另外一个 ...