对于每个连通块欧拉回路存在的条件

无向图:只存在两个或者零个度数为奇数的点

有向图:每个点的入度等于出度或者至多有两个点入度不等于出度且一个出度比入度多一另一个入度比出度多一

HDU 多校第二场 C.cover

题意:给你一个无向图 问你一笔画最多多少次能把所有边覆盖(走过的边不能走)

并且输出每个一笔画的路径(边的下标)

解:给每一对度数为奇数的点连上一条边使之度数变成偶数 然后跑欧拉回路

欧拉回路是从一个正确的点出发然后暴力dfs遇到走过的边就不要走

/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-;
const int dir[][] = {{, }, {, }, {, -}, { -, }, {, }, {, -}, { -, -}, { -, }};
const int mod = 1e9 + , gakki = + + + + 1e9;
const int MAXN = 1e5 + , MAXM = 2e5 + , MAXQ = , INF = 1e9;
const ll LLINF = (1LL << );
int to[MAXM << ], nxt[MAXM << ], Head[MAXN], tot = ;
int index[MAXM << ];
bool used[MAXM << ];
inline void addedge(int u, int v, int x)
{
if (u == v)
{
return ;
}
to[++tot] = v;
nxt[tot] = Head[u];
index[tot] = x;
used[tot] = false;
Head[u] = tot;
}
inline void read(int &v)
{
v = ;
char c = ;
int p = ;
while (c < '' || c > '')
{
if (c == '-')
{
p = -;
}
c = getchar();
}
while (c >= '' && c <= '')
{
v = (v << ) + (v << ) + c - '';
c = getchar();
}
v *= p;
}
int n, m;
int du[];
bool visit[];
int ans = ;
vector<int> anser[];
int cnt = ;
void dfs(int x)
{
int nowindex;
visit[x] = true;
for (int v, i = Head[x]; i; i = nxt[i])
{
v = to[i];
if (used[i])
{
continue;
}
used[i] = used[i ^ ] = true;
nowindex = index[i];
//cout << x << " to " << v << " index " << nowindex << " i " << i << endl;
dfs(v);
if (nowindex == )
{
anser[++cnt].clear();
}
else
{
anser[cnt].push_back(-nowindex);
}
}
}
int main()
{
ios_base::sync_with_stdio();
cin.tie(); int u, v;
while (~scanf("%d %d", &n, &m))
{
cnt = ;
tot = ;
for (int i = ; i <= n; i++)
{
du[i] = ;
visit[i] = false;
Head[i] = ;
}
for (int i = ; i <= m; i++)
{
read(u), read(v);
addedge(u, v, i);
addedge(v, u, -i);
du[u]++, du[v]++;
}
int aim = ;
for (int i = ; i <= n; i++)
{
if (du[i] & )
{
if (aim)
{
addedge(aim, i, );
addedge(i, aim, );
aim = ;
}
else
{
aim = i;
}
}
}
for (int i = ; i <= n; i++)
{
if (!visit[i] && (du[i] & ))
{
anser[++cnt].clear();
dfs(i);
cnt--;
}
}
for (int i = ; i <= n; i++)
{
if (!visit[i] && du[i])
{
anser[++cnt].clear();
dfs(i);
}
}
printf("%d\n", cnt);
for (int i = ; i <= cnt; i++)
{
printf("%d ", anser[i].size());
for (int j = ; j < anser[i].size(); j++)
{
printf("%d", anser[i][j]);
if (j == anser[i].size() - )
{
putchar('\n');
}
else
{
putchar(' ');
}
}
}
}
return ;
}

Euler-path的更多相关文章

  1. poj 题目分类(2)

    初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(poj329 ...

  2. ACM常用算法及练习(1)

    ACM常用算法及练习 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码,因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打出来. 1.最短 ...

  3. 欧拉路径Hrbust1351

    http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1351 这道题先利用并查集的知识点, ...

  4. 48. Rotate Image

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  5. how to learn algorithms(ZAC)

    (这一篇觉得写得很棒,故拷过来以便慢慢看,细细体会,详情请访问http://blog.csdn.net/shenmen123456/article/details/6575647) 第一阶段:练经典常 ...

  6. ACM学习

    转:ACM大量习题题库   ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库.   US ...

  7. ACM计划

    原文 :http://027xbc.blog.163.com/blog/static/128159658201141371343475/ ACM主要是考算法的,主要时间是花在思考算法上,不是花在写程序 ...

  8. 《算法》第四章部分程序 part 8

    ▶ 书中第四章部分程序,包括在加上自己补充的代码,图中找欧拉路径 ● 无向图中寻找欧拉路径,只注释了与欧拉环不同的地方 package package01; import edu.princeton. ...

  9. (转载)ACM训练计划,先过一遍基础再按此拼搏吧!!!!

    ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. USACO ht ...

  10. NPC问题及其解决方法(回溯法、动态规划、贪心法、深度优先遍历)

    NP问题(Non-deterministic Polynomial ):多项式复杂程度的非确定性问题,这些问题无法根据公式直接地计算出来.比如,找大质数的问题(有没有一个公式,你一套公式,就可以一步步 ...

随机推荐

  1. GPU编程shader之正余弦波和幂/指数函数

    先上一个demo代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  2. gin框架教程:代码系列demo地址

    gin框架教程代码地址: https://github.com/jiujuan/gin-tutorial demo目录: 01quickstart 02parameter 03route 04midd ...

  3. springboot和springcloud版本冲突问题

    最近搭建eureka项目,出现boot和cloud版本不匹配错误,记录下来 2019-12-06 14:00:20.043 ERROR 180780 --- [ main] o.s.boot.Spri ...

  4. react中路由跳转push与replace的区别

    路由跳转,replace / push 区别 push: a-b-c 可以回到上一级 例: this.props.history.push('路由地址') replace: a-b-c 回不到上一级 ...

  5. 封装自己的jquery插件

    自己尝试封装的一个在工作当中使用的多级弹窗插件: ;(function ($, window, document) { //用一个自调用匿名函数把插架代码包裹起来,防止代码污染 $.fn.multi ...

  6. [python] 执行 dos 命令

    python的os模块 os模块调用CMD命令有两种方式:os.popen(),os.system(). 都是用当前进程来调用. os.system是无法获取返回值的.当运行结束后接着往下面执行程序. ...

  7. docker 使用阿里云免费仓库

    阿里云为开发人员提供了免费的仓库~~ 登录阿里云 ,选择容器镜像服务,当前是2019/08/13 ,之后不知道阿里云控制台菜单会不会调整哈 进入容器镜像服务菜单,创建仓库,需要绑定git仓库 怎么上传 ...

  8. [转帖]IBM收购红帽价格是多少?是否会形成垄断企业?会存在什么不安因素?

    http://www.techweb.com.cn/it/2019-07-10/2743776.shtml 国产的linux 用centos源的 如何是好呢.. 蓝色巨人IBM官方宣布,已经正式完成对 ...

  9. python实现更换电脑桌面壁纸,锁屏,文件加密方式

    python实现更换壁纸和锁屏代码 #控制windows系统 import win32api,win32con,win32gui # 可以利用python去调用dll动态库的包.嵌入式开发 from ...

  10. Spring 中的bean 是线程安全的吗?

    结论: 不是线程安全的 Spring容器中的Bean是否线程安全,容器本身并没有提供Bean的线程安全策略,因此可以说Spring容器中的Bean本身不具备线程安全的特性,但是具体还是要结合具体sco ...