HDU - 6311 Cover (欧拉路径)
题意:有最少用多少条边不重复的路径可以覆盖一个张无向图。
分析:对于一个连通块(单个点除外),如果奇度数点个数为 k,那么至少需要max{k/2,1} 条路径。将奇度数的点两两相连边(虚边),然后先从奇度数的点出发,搜索由其出发的欧拉回路。需要将遍历的边和其反向边打标记,并在DFS退栈的时候记录边的编号(前向星的存储是访问后加入的边),若该边是自己添加的虚边,那么说明实际上这次DFS搜索到的是一条欧拉通路,那么结果还需额外+1,所以对所有奇数点DFS过后,得到的结果就是max{k/2,1}。
再从未被访问过的偶数顶点出发搜索由其出发的欧拉回路,每一次DFS就是找到了一条回路。
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
const int maxn =1e5+;
struct Edge{
int to,id,next;
bool f;
}edges[maxn<<];
int tot,head[maxn],cnt;
bool vis[maxn];
vector<int> res[maxn];
int deg[maxn]; void init()
{
tot=;
cnt=;
memset(deg,,sizeof(deg));
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
} void AddEdge(int u,int v ,int id)
{
edges[tot].f = ;edges[tot].to=v;edges[tot].id = id;edges[tot].next =head[u];
head[u]=tot++;
} void dfs(int u)
{
vis[u]=true;
//cout<<u<<" in "<<cnt<<endl;
for(int i=head[u];~i;i=edges[i].next){
int v =edges[i].to,id =edges[i].id;
if(!edges[i].f){
edges[i].f = edges[i^].f = true; //将边和反向边标记
dfs(v);
if(id) res[cnt].push_back(-id); //退栈记录边的id
else cnt++; //扫到虚边,那么路径加1
//cout<<u<<" out "<<cnt<<endl;
}
}
} void Print()
{
printf("%d\n",cnt);
for(int i=;i<=cnt;++i){
printf("%d",res[i].size());
int k = res[i].size();
for(int j=;j<k;++j) printf(" %d",res[i][j]);
printf("\n");
res[i].clear();
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T,N,M,u,v,tmp;
while(scanf("%d%d",&N,&M)==){
init();
for(int i=;i<=M;++i){
scanf("%d%d",&u,&v);
deg[u]++,deg[v]++;
AddEdge(u,v,i);
AddEdge(v,u,-i);
}
u=;
for(int i=;i<=N;++i){
if(deg[i]&){
if(u){
AddEdge(u,i,);
AddEdge(i,u,);
u=;
} //将奇度数点两两连边
else u=i;
}
}
for(int i=;i<=N;++i){
if(!vis[i] && (deg[i]&)){
cnt++;
dfs(i);
cnt--;
}
}
for(int i=;i<=N;++i){
if(!vis[i] && deg[i]){
cnt++;
dfs(i);
}
}
Print();
}
return ;
}
HDU - 6311 Cover (欧拉路径)的更多相关文章
- HDU 6311 Cover (无向图最小路径覆盖)
HDU 6311 Cover (无向图最小路径覆盖) Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- HDU - 6311 Cover(无向图的最少路径边覆盖 欧拉路径)
题意 给个无向图,无重边和自环,问最少需要多少路径把边覆盖了.并输出相应路径 分析 首先联通块之间是独立的,对于一个联通块内,最少路径覆盖就是 max(1,度数为奇数点的个数/2).然后就是求欧拉路 ...
- HDU - 6311:Cover(欧拉回路,最少的一笔画覆盖无向图)
The Wall has down and the King in the north has to send his soldiers to sentinel. The North can be r ...
- hdu 5386 Cover (暴力)
hdu 5386 Cover Description You have an matrix.Every grid has a color.Now there are two types of oper ...
- HDU 6311 最少路径覆盖边集 欧拉路径
Cover Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU6311 Cover (欧拉路径->无向图有最少用多少条边不重复的路径可以覆盖一个张无向图)
题意:有最少用多少条边不重复的路径可以覆盖一个张无向图 ,输出每条路径的边的序号 , 如果是反向就输出-id. 也就是可以多少次一笔画的方式画完这个无向图. 题解:我们已知最优胜的情况是整个图是欧拉图 ...
- HDU 5386 Cover(模拟)
Cover Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Subm ...
- HDU 5386 Cover
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5386 题目大意:给一个初始矩阵(n×n).一个目标矩阵(n×n)和m个操作,要求找到一种操作顺序,使初 ...
- hdu 6311 欧拉回路
题意:求一个图(不一定联通)最小额外连接几条边,使得可以一笔画出来 大致做法 1.找出联通块 2.统计每一个连通块里面度数为奇数的点的个数, 有一个性质 一个图能够用一笔画出来,奇数点的个数不超过2个 ...
随机推荐
- 临界区(Critical Section)的封装和使用示例
向我老大致敬! 这个做法其实是抄我老大的.服务器中,多线程经常需要使用临界区,为了简化代码的使用,把临界区封装为 CThreadLockHandle 类,通过封装,使用临界区资源每次只需要一行代码, ...
- transform使用参考指南
transform使用参考指南 2010-09-18 16:06:33| 分类: html5+css3|字号 订阅 语法 transform :none | <transform-fu ...
- SVD分解与数据压缩
SVD的几何解释:http://blog.csdn.net/dinosoft/article/details/37884597 上文未证明为什么AAT的特征向量就是要找的v 这里有个简单的说明: SV ...
- 《linux 内核全然剖析》 include/asm/io.h
include/asm/io.h #define outb(value,port) \ __asm__ ("outb %%al,%%dx"::"a" (valu ...
- RedHat Ent 6.5 64bit编译安装hadoop2.4.1
RedHat Ent 6.5 64bit编译安装hadoop2.4.1 感谢原帖:http://blog.csdn.net/w13770269691/article/details/16883663/ ...
- asp.net session丢失的解决方法小结
现在我就把原因和解决办法写出来. ASP.NET Session丢失原因: 由于Asp.net程序是默认配置,所以Web.Config文件中关于Session的设定如下: < sessionSt ...
- React资料
基于ReactNative开发的APPhttp://reactnative.cn/cases.htmlhttp://www.cnblogs.com/qiangxia/p/5584622.html F8 ...
- ASP.NET Web API中的路由
ASP.NET Web API的默认路由在App_Start目录中的WebApiConfig.cs文件中定义的. public static class WebApiConfig { public s ...
- python中WSGI是什么
uswgi学习文档 http://uwsgi-docs-cn.readthedocs.io/zh_CN/latest/WSGIquickstart.html WSGI是什么? WSGI,全称 Web ...
- jQuery Validation Engine 表单验证,自定义规则验证方法
jQuery Validation Engine 表单验证说明文档http://code.ciaoca.com/jquery/validation-engine/ js加到jquery.validat ...