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

时间:2022-08-31 12:17:56

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 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

题目解析

题意是问一个图最少几笔能够画完,把每一笔输出出来。

有一个规律很容易发现,就是一个不存在欧拉回路的图,需要奇点数/2笔画完,具体是怎么操作的呢

  1. 建图
  2. 每两个奇点之间连一条边
  3. 从原奇点开始,DFS欧拉回路,删去加进来的边,得到结果
  4. 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 (无向图最小路径覆盖)的更多相关文章

  1. &lpar;step6&period;3&period;4&rpar;hdu 1151&lpar;Air Raid——最小路径覆盖&rpar;

    题意:     一个镇里所有的路都是单向路且不会组成回路. 派一些伞兵去那个镇里,要到达所有的路口,有一些或者没有伞兵可以不去那些路口,只要其他人能完成这个任务.每个在一个路口着陆了的伞兵可以沿着街去 ...

  2. HDU 4160 Dolls (最小路径覆盖&equals;顶点数-最大匹配数)

    Dolls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  3. hdu 1151 Air Raid 最小路径覆盖

    题意:一个城镇有n个路口,m条路.每条路单向,且路无环.现在派遣伞兵去巡逻所有路口,伞兵只能沿着路走,且每个伞兵经过的路口不重合.求最少派遣的伞兵数量. 建图之后的就转化成邮箱无环图的最小路径覆盖问题 ...

  4. hdu6311 &sol;&sol;&sol; 欧拉路径 无向图最小路径覆盖 输出正反路径

    题目大意: 给定n m 为图的点数和边数 接下来m行 u v 为u到v有一条边 要求最少几笔能画完图的所有边 输出每笔画过的路径编号 正数编号正向 负数编号反向 题解:https://www.cnbl ...

  5. hdu 1151 Air Raid&lpar;二分图最小路径覆盖)

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

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

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

  7. 缩点&plus;最小路径覆盖 hdu 3861

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意:输入t,表示t个样例.接下来每个样例第一行有两个数n,m表示点数和有向边的数量,接下来输入 ...

  8. HDU 3861&period;The King’s Problem 强联通分量&plus;最小路径覆盖

    The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. (匹配 最小路径覆盖)Air Raid --hdu --1151

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1151 http://acm.hust.edu.cn/vjudge/contest/view.action ...

随机推荐

  1. HTML中让表单input等文本框为只读不可编辑的方法

    有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使<input type="text" name="input1" value=&qu ...

  2. 指令 scope

    angular学习笔记(三十)-指令(8)-scope <!DOCTYPE html> <html ng-app="myApp"> <head> ...

  3. 在Eclipse中使用JUnit4进行单元測试(0基础篇)

    本文绝大部分内容引自这篇文章: http://www.devx.com/Java/Article/31983/0/page/1 我们在编写大型程序的时候,须要写成千上万个方法或函数,这些函数的功能可能 ...

  4. Sublime Text3 运行Python 出现Error:Decode error - output not utf-8

    问题描述: Sublime Text 3 在build Python时,如果python源代码输出有中文,例如"print('中文')",Sublime Text 会报 [Deco ...

  5. Webpack系列-第一篇基础杂记

    前言 公司的前端项目基本都是用Webpack来做工程化的,而Webpack虽然只是一个工具,但内部涉及到非常多的知识,之前一直靠CV来解决问题,之知其然不知其所以然,希望这次能整理一下相关的知识点. ...

  6. is&lowbar;valid校验机制

    先来归纳一下整个流程 (1)首先is_valid()起手,看seld.errors中是否值,只要有值就是flase(2)接着分析errors.里面判断_errors是都为空,如果为空返回self.fu ...

  7. 如何在Windows中通过Cygwin来使用Linux命令行

    PowerShell的出现让Windows的命令行工具有了很大的改进.但是多年以来,Linux一直拥有很多有用的终端.在这里通过Cygwin你可以同时拥有上面两种命令行工具,Cygwin是一个可以在W ...

  8. Java中动态代理方式:

    JDK中生成代理对象的API 代理类所在包:java.lang.reflect.ProxyJDK实现代理只需要使用newProxyInstance方法,但是该方法需要接收三个参数,完整的写法是: st ...

  9. Intellij新建Spring项目引入用户目录下的Spring jar包

    首先,在IntelliJ IDEA中新建module,选择Spring应用:   在初次使用时,如果IDE检测到本地没有spring核心库,则会在新建过程中下载对应库文件,在使用spring框架时,可 ...

  10. C语言的知识与能力的自评

    1.我希望将来上班的地方是自己所感兴趣的,正在寻找自己感兴趣的,并且正在普及IT行业的相关知识. 2.我认为学习就是一个自我成长和自我提升以及认识世界的方法,学习的作用是可以不断的提升对这个世界的认识 ...