HDU 6311 Cover (无向图最小路径覆盖)
HDU 6311 Cover (无向图最小路径覆盖)
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1967 Accepted Submission(s): 442
Special JudgeProblem 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≤100000Output
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
题目解析
题意是问一个图最少几笔能够画完,把每一笔输出出来。
有一个规律很容易发现,就是一个不存在欧拉回路的图,需要奇点数/2笔画完,具体是怎么操作的呢
- 建图
- 每两个奇点之间连一条边
- 从原奇点开始,DFS欧拉回路,删去加进来的边,得到结果
- DFS剩下的联通块,得到结果
代码
#include<iostream>
#include<vector>
#include<cstring>
#define CLR(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = 100010;
struct Edge
{
int from,to,next;
int num;
int d;
}edge[maxn<<4];
int vis[maxn<<4];
int head[maxn];
int du[maxn];
int tol;
int n,m,cnt;
vector<int> st;
vector<int> ans[maxn];
void init()
{
cnt = 1;
tol = 0;
CLR(head,-1);
CLR(vis,0);
CLR(du,0);
for(int i = 1; i <= maxn; i++)
ans[i].clear();
st.clear();
}
void addedge(int u,int v,int num,int d)
{
edge[tol].from = u;
edge[tol].to = v;
edge[tol].num = num;
edge[tol].d = d;
edge[tol].next = head[u];
head[u] = tol++;
}
void dfs(int st)
{
for(int i = head[st]; i!=-1; i = edge[i].next){
if( vis[edge[i].num]) continue;
vis[edge[i].num] = 1;
dfs(edge[i].to);
if(edge[i].d == 0){
cnt++;
}
else{
ans[cnt].push_back(edge[i].d*edge[i].num*-1);
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(int i = 1; i <= m; i++){
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v,i,1);
addedge(v,u,i,-1);
du[v]++;
du[u]++;
}
int sig = m;
int tmp = 0;
for(int i = 1; i <= n; i++){
if(tmp == 0 && du[i]%2 == 1){
tmp = i;
du[tmp]++;
}
else if(tmp && du[i]%2 == 1){
addedge(i,tmp,++sig,0);
addedge(tmp,i,sig,0);
du[i]++;
st.push_back(tmp);
tmp = 0;
}
}
for(int i = 0; i < st.size(); i++){
dfs(st[i]);
}
for(int i = 0; i < tol; i++){
if(!vis[edge[i].num]){
dfs(edge[i].from);
cnt++;
}
}
printf("%d\n",cnt-1);
for(int i = 1; i < cnt; i++){
printf("%d ",ans[i].size());
for(int j = 0; j < ans[i].size(); j++){
printf("%d ",ans[i][j]);
}
printf("\n");
}
}
return 0;
}
HDU 6311 Cover (无向图最小路径覆盖)的更多相关文章
- (step6.3.4)hdu 1151(Air Raid——最小路径覆盖)
题意: 一个镇里所有的路都是单向路且不会组成回路. 派一些伞兵去那个镇里,要到达所有的路口,有一些或者没有伞兵可以不去那些路口,只要其他人能完成这个任务.每个在一个路口着陆了的伞兵可以沿着街去 ...
- HDU 4160 Dolls (最小路径覆盖=顶点数-最大匹配数)
Dolls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
- hdu 1151 Air Raid 最小路径覆盖
题意:一个城镇有n个路口,m条路.每条路单向,且路无环.现在派遣伞兵去巡逻所有路口,伞兵只能沿着路走,且每个伞兵经过的路口不重合.求最少派遣的伞兵数量. 建图之后的就转化成邮箱无环图的最小路径覆盖问题 ...
- hdu6311 /// 欧拉路径 无向图最小路径覆盖 输出正反路径
题目大意: 给定n m 为图的点数和边数 接下来m行 u v 为u到v有一条边 要求最少几笔能画完图的所有边 输出每笔画过的路径编号 正数编号正向 负数编号反向 题解:https://www.cnbl ...
- hdu 1151 Air Raid(二分图最小路径覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=1151 Air Raid Time Limit: 1000MS Memory Limit: 10000K To ...
- HDU 3861 The King’s Problem 最小路径覆盖(强连通分量缩点+二分图最大匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 最小路径覆盖的一篇博客:https://blog.csdn.net/qq_39627843/ar ...
- 缩点+最小路径覆盖 hdu 3861
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意:输入t,表示t个样例.接下来每个样例第一行有两个数n,m表示点数和有向边的数量,接下来输入 ...
- HDU 3861.The King’s Problem 强联通分量+最小路径覆盖
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- (匹配 最小路径覆盖)Air Raid --hdu --1151
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1151 http://acm.hust.edu.cn/vjudge/contest/view.action ...
随机推荐
- Contest2156 - 2019-3-7 高一noip基础知识点 测试2 题解版
传送门 预计得分:100+70+100+50=320 实际得分100+63+77+30=270 Ctrl_C+Ctrl_V时不要粘贴翻译的,直接粘原文, In a single line of the ...
- IN-子查询
为什么需要子查询? 现实中,很多情况需要进行以下条件的判断 集合成员资格 某一元素是否是某一个集合的成员 集合之间的比较 某一个集合是否包含另一个集合 集合基数的测试 测试集合是否为空 测试集合是否存 ...
- MyBatis联表查询
MyBatis逆向工程主要用于单表操作,那么需要进行联表操作时,往往需要我们自己去写sql语句. 写sql语句之前,我们先修改一下实体类 Course.java: public class Cours ...
- Django 实现list页面检索
在list.html写入from表单 在views渲染list方法写入,从前台获取的searchtitle根据name实现检索
- Node.js使用jszip实现打包zip压缩包
一.前言 最近有这样的一个需求,需要把两个同名的.mtl文件和.obj文件打包成一个同名的.zip压缩包.刚开始文件不多的时候,只有几个,或者十几个,甚至二三十个的时候,还能勉强接受手动修改,但是随着 ...
- [Harbor]Harbor简要介绍
前一段时间写过一篇文章:[Kubernetes]CentOS7下搭建Harbor仓库,只是知道了如何搭建,但是对于背后的整体架构还不是太清楚,这篇文章就来讲讲. 默认情况下,Harbor运行起来后有如 ...
- CEC和ARC介绍
众所周知,HDMI作为一个数字化视频音频的接收标准,是可以同时传输视频和音频的,当然随着HDMI版本的提升,它的功能也一直在增强.事实上 当HDMI升级到1.3时,人们就发现了HDMI多了一个CEC功 ...
- GPS车辆监控系统的启动方式
我们通常用到的GPS车辆监控系统都有哪些启动方式,又有什么区别呢?通常GPS车辆监控系统都有热启.冷启.温启的技术指标,现参考如下:GPS开机定位分为冷启动.温启动和热启动三种:一.冷启动:以下几种情 ...
- 【算法】【python实现】二叉树深度、广度优先遍历
二叉树的遍历,分为深度优先遍历,以及广度优先遍历. 在深度优先遍历中,具体分为如下三种: 先序遍历:先访问根节点,再遍历左子树,再遍历右子树: 中序遍历:先遍历左子树,再访问根节点,再遍历右子树: 后 ...
- Flask开发微电影网站(六)
1. 后台管理登录功能实现 1.1 后台管理页面登录表单LoginForm 在app的admin目录下创建forms.py文件,用来保存admin蓝图中需要使用到的表单 from flask_wtf ...