Euler-path
对于每个连通块欧拉回路存在的条件
无向图:只存在两个或者零个度数为奇数的点
有向图:每个点的入度等于出度或者至多有两个点入度不等于出度且一个出度比入度多一另一个入度比出度多一
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的更多相关文章
- poj 题目分类(2)
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(poj329 ...
- ACM常用算法及练习(1)
ACM常用算法及练习 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码,因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打出来. 1.最短 ...
- 欧拉路径Hrbust1351
http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1351 这道题先利用并查集的知识点, ...
- 48. Rotate Image
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- how to learn algorithms(ZAC)
(这一篇觉得写得很棒,故拷过来以便慢慢看,细细体会,详情请访问http://blog.csdn.net/shenmen123456/article/details/6575647) 第一阶段:练经典常 ...
- ACM学习
转:ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. US ...
- ACM计划
原文 :http://027xbc.blog.163.com/blog/static/128159658201141371343475/ ACM主要是考算法的,主要时间是花在思考算法上,不是花在写程序 ...
- 《算法》第四章部分程序 part 8
▶ 书中第四章部分程序,包括在加上自己补充的代码,图中找欧拉路径 ● 无向图中寻找欧拉路径,只注释了与欧拉环不同的地方 package package01; import edu.princeton. ...
- (转载)ACM训练计划,先过一遍基础再按此拼搏吧!!!!
ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. USACO ht ...
- NPC问题及其解决方法(回溯法、动态规划、贪心法、深度优先遍历)
NP问题(Non-deterministic Polynomial ):多项式复杂程度的非确定性问题,这些问题无法根据公式直接地计算出来.比如,找大质数的问题(有没有一个公式,你一套公式,就可以一步步 ...
随机推荐
- spring boot系列(四)spring boot 配置spring data jpa (保存修改删除方法)
spring boot 使用jpa在pom.xml在上文中已经介绍过.在这里直接介绍各个类文件如何编写: 代码结构: domain(存放实体类文件): repository(存放数据库操作文件,相当于 ...
- pytorch基础问题
本文将自己在pytorch学习中遇见的各种问题整理起来,并且持续更新. 1:torch.Tensor和torch.tensor的区别 开始使用torch.tensor和torch.Tensor的时候发 ...
- Java的Comparable接口
Comparable接口提供比较对象大小功能,实现了此接口的类的对象比较大小将通过接口提供的compareTo方法. 此方法的返回int类型,分三种情况. 返回正数,当前对象大于目标对象 返回负数,当 ...
- java线程中start和run的区别
public class Test1 extends Thread { @Override public void run() { while (true) { System.out.println( ...
- 【Python开发】urllib2.urlopen超时问题
原帖地址:http://hi.baidu.com/yss1983/item/933fbe45a09c43e01381da06 问题描述: 没有设置timeout参数,结果在网络环境不好的情况下 ...
- redhat 7 防火墙配置
没有iptables 用systemctl stop firewalld
- Duilib的控件拖拽排序,支持跨容器拖拽(网易云信版本)
完整代码见:https://github.com/netease-im/NIM_Duilib_Framework/pull/151 核心代码(思路): appitem.h #pragma once # ...
- hue改下载行数
参考: https://blog.csdn.net/lingbo229/article/details/85991230 修改hue所在机器的默认配置后,重启hue即可 find / -name be ...
- [c++] 用宏定义一个函数
要点:变量都用括号括起来,防止出错,结尾不需要;.在实际编程中,不推荐把复杂的函数使用宏,不容易调试.多行用\ 要写好C语言,漂亮的宏定义是非常重要的.宏定义可以帮助我们防止出错,提高代码的可移植性和 ...
- 打印指针要用%p而不要用%x
注意: 打印指针要用%p而不要用%x 原因: https://boredzo.org/blog/archives/2007-01-23/please-do-not-use-percent-x-for- ...