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 ):多项式复杂程度的非确定性问题,这些问题无法根据公式直接地计算出来.比如,找大质数的问题(有没有一个公式,你一套公式,就可以一步步 ...
随机推荐
- Control的Invoke和BeginInvoke
转载:https://www.cnblogs.com/c2303191/articles/826571.html 近日,被Control的Invoke和BeginInvoke搞的头大,就查了些相关的资 ...
- 使用NLog的最佳实践
1. Logger应该在每个类中初始化为静态 创建一个新的Logger类是有有开销的,因为它需要获取一些锁和分配对象和内存. 因此推荐像下面一样使用Logger: namespace MyNamesp ...
- 分布式架构-Redis 从入门到精通 完整案例 附源码
导读 篇幅较长,干货十足,阅读需要花点时间,全部手打出来的字,难免出现错别字,敬请谅解.珍惜原创,转载请注明出处,谢谢~! NoSql介绍与Redis介绍 什么是Redis? Redis是用C语言开发 ...
- Msf小结
Msfvenom是有效负载生成和编码的组合. 生成攻击载荷 Linux下反弹Meterpreter shell 1 msfvenom -p linux/x86/meterpreter/reverse_ ...
- linux中su和sudo区别
su切换用户,切换成root用户,要输入root用户的密码 su - 用户名 sudo 涉及到 /etc/sudoers文件 ,内容如下: # User privilege specificatio ...
- channel 介绍
!!!1.Memory Channel 内存通道 事件将被存储在内存中的具有指定大小的队列中. 非常适合那些需要高吞吐量但是失败是会丢失数据的场景下. 属性说明: !type – 类型,必须是“m ...
- gzip 命令
NAME gzip -- compression/decompression tool using Lempel-Ziv coding (LZ77) SYNOPSIS gzip [-cdfhkLlNn ...
- “automation服务器不能创建对象”的问题的解决方案大全
本人工作中的应用系统都是jsp的,大量javascript程序,一旦出“automation服务器不能创建对象”问题,大量报表及查询无法保存,苦思冥想.千尝万试,终于将其搞定,现将相关方案与大家共享. ...
- 小记---------spring框架之IOC理解
Spring是一个开源框架,是一个轻量级的Java开发框架. Spring的核心是控制发转(IOC)和面向切面(AOP) 控制发转(IOC):指的是 对象的创建权反转(交给)给 Spring. 作 ...
- hello2源代码分析
String username = request.getParameter("username");/* *以 String 形式返回请求参数"username&quo ...