HDU 6311 最少路径覆盖边集 欧拉路径
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
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.
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
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.
解析 对于每个连通块最少路径覆盖就是 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 最少路径覆盖边集 欧拉路径的更多相关文章
- poj 1422 Air Raid 最少路径覆盖
题目链接:http://poj.org/problem?id=1422 Consider a town where all the streets are one-way and each stree ...
- hdu 1151 最小路径覆盖
先说说最小路径覆盖的定义 定义:在一个有向图中,找出最少的路径,使得这些路径,经过每一个点,且每一个点只与一条路径相关联, 由上面得出: 1.一个单独的点是一个路径 2:如果有路径a,b,c....f ...
- Antenna Placement(匈牙利算法 ,最少路径覆盖)
Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6991 Accepted: 3466 ...
- Hdu 1068 最小路径覆盖
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU - 6311 Cover(无向图的最少路径边覆盖 欧拉路径)
题意 给个无向图,无重边和自环,问最少需要多少路径把边覆盖了.并输出相应路径 分析 首先联通块之间是独立的,对于一个联通块内,最少路径覆盖就是 max(1,度数为奇数点的个数/2).然后就是求欧拉路 ...
- HDU 6311 Cover (无向图最小路径覆盖)
HDU 6311 Cover (无向图最小路径覆盖) Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- (hdu step 6.3.3)Air Raid(最小路径覆盖:求用最少边把全部的顶点都覆盖)
题目: Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- 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 ...
随机推荐
- (转)Synopsys工具简介
DC Ultra--Design Compiler的最高版本 在Synopsys软件中完整的综合方案的核心是DC UltraTM,对所有设计而言它也是最好级别的综合平台.DC Ultra添加了全面的数 ...
- Keil简介
最早接触Keil是学习开发8051系列的单片机.Keil C51是Keil公司出品的51系列兼容单片机C语言软件开发系统,与汇编相比,C语言在功能上.结构性.可读性.可维护性上有明显的优势,因而易学易 ...
- sql 删除重复数据
DELETE a FROM tbBuilding a WHERE EXISTS (SELECT 1 FROM tbBuilding b WHERE b.Province = a.Province AN ...
- 【OpenCV】motion blur 的简单实现
先推荐界面比较丑,但是还不错的在线图片处理网站: http://www168.lunapic.com/editor/ 由于最近在做毕设了,结合前面关于图像处理和机器学习的操作,想做一些好玩的东西,其中 ...
- Zend Studio 修改“代码字体和大小”
- HashMap和HashTable的理解与区别
Hashtable是java一开始发布时就提供的键值映射的数据结构,而HashMap产生于JDK1.2.虽然Hashtable比HashMap出现的早一些,但是现在Hashtable基本上已经被弃用了 ...
- mongodb windows 开机启动
1)新建存放db E:\mongodb\data\db 2)新建日志文件 E:\mongodb\logs\log.txt 3)管理员运行Cmd 4)CD mongodb安装路径 5)运行命令 mong ...
- 任务二:零基础HTML及CSS编码(一)
面向人群: 零基础或初学者 难度: 简单 重要说明 百度前端技术学院的课程任务是由百度前端工程师专为对前端不同掌握程度的同学设计.我们尽力保证课程内容的质量以及学习难度的合理性,但即使如此,真正决定课 ...
- (转)浅谈trie树
浅谈Trie树(字典树) Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问 ...
- C++ 给自己的扫盲笔记
1.运算符new分配内存的格式: 指针变量名 = new 类型: 如分配一个20字节的name变量 :name = new char[20]; 2.strlen(s);函数: 返回字符串s的长度 ...