Uvaoj10054 - The Necklace

/*
题意:打印欧拉回路!
思路:开始时不明白,dfs为什么是后序遍历?
因为欧拉回路本身是一条回路,那么我们在dfs时,可能存在提前找到回路,这条回路可能不是欧拉回路,
因为没有遍历完成所有的边!如果写成前序遍历的话,存储起来的路径就不是一条完整的路径了!它有可能
是多条回路组成的!答案就是错误 的!如果是后序遍历的话,当dfs是遇到了回路,那么就退出当前栈的搜索!
去寻找其他的路径!最终得到的只有一条回路路径!-->欧拉回路~!
*/
#include<iostream>
#include<cstring>
#define M 55
#define Max 0x3f3f3f3f
using namespace std; int cnt[M][M];
int deg[M];
int map[M][M];
int x; struct Point{
int x, y;
Point(){} Point(int x, int y){
this->x=x;
this->y=y;
}
}; Point p[];
int top; void dfs(int u){
if(!deg[u]) return;
for(int i=; i<=; ++i)
if(map[u][i] && cnt[u][i]){
--cnt[u][i];
--cnt[i][u];
--deg[u];
--deg[i];
dfs(i);
p[++top]=Point(u, i);
}
} int main(){
int t, n, k=;
cin>>t;
while(t--){
cin>>n;
x=Max;
memset(cnt, , sizeof(cnt));
memset(map, , sizeof(map));
memset(deg, , sizeof(deg));
for(int i=; i<=n; ++i){
int u, v;
cin>>u>>v;
deg[u]++;
deg[v]++;
map[u][v]=;
map[v][u]=;
cnt[u][v]++;
cnt[v][u]++;
if(x>u) x=u;
if(x>v) x=v;
}
int ok=;
for(int i=; i<=; ++i)
if(deg[i]%!=){
ok=;
break;
}
cout<<"Case #"<<++k<<endl;
if(ok){
cout<<"some beads may be lost"<<endl;
if(t) cout<<endl;
continue;
}
top=;
dfs(x);
if(top==n){
for(top; top>=; --top)
cout<<p[top].x<<" "<<p[top].y<<endl;
}
else cout<<"some beads may be lost"<<endl;
if(t) cout<<endl;
}
return ;
}
Uvaoj10054 - The Necklace的更多相关文章
- HDU5730 Shell Necklace(DP + CDQ分治 + FFT)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of ...
- 2016 Multi-University Training Contest 1 H.Shell Necklace
Shell Necklace Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- hdu 5727 Necklace dfs+二分图匹配
Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- USACO section1.1 Broken Necklace
/* ID: vincent63 LANG: C TASK: beads */ #include <stdio.h> #include<stdlib.h> #include&l ...
- [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链
[BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链 试题描述 欢乐岛上众多新奇的游乐项目让小可可他们玩的非常开心.现在他们正在玩比赛串项链的游戏,谁串的最快就能得到 ...
- POJ 1286 Necklace of Beads(Polya原理)
Description Beads of red, blue or green colors are connected together into a circular necklace of n ...
- Accepted Necklace
Accepted Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 2660 Accepted Necklace
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2660 Accepted Necklace Description I have N precious ...
随机推荐
- 结对项目——高级四则运算检验器记录(168 & 187)
首先,上图(*+﹏+*)~@ 1.如何看待结对编程 结对编程优点: 1.两个人能够相互支持,相互监督,客服编程过程中可能出现的烦躁的情况0_0. 2.在开发功能的同时,伴随了UnitTest的进行,可 ...
- Debian 7 安装 wireshark
安装过程很简单: $ sudo apt-get install wireshark 其中会弹出一个对话框: ┌─────────────────────┤ Configuring wireshark- ...
- Jetty源码分析(一)
一.目的 1.了解jetty组成架构: 2.学习jetty启动过程: 3.学习请求访问过程: 4.学习jetty内各模块作用,学习各模块内部代码: 二.jetty版本 本文所学习的jetty版本为:9 ...
- Python学习之路-Day4
1.函数 函数定义 def func(aa): def:表示函数的关键字 func:函数名,即函数的名称,可根据函数名调用函数 print('.....') prin ...
- 第51讲:Scala中链式调用风格的实现代码实战及其在Spark编程中的广泛运用
今天学习了下scala中的链式调用风格的实现,在spark编程中,我们经常会看到如下一段代码: sc.textFile("hdfs://......").flatMap(_.spl ...
- firefox安装firebug\XPath Checker
XPath Checker安装,打开下面地址安装: https://addons.mozilla.org/en-US/firefox/addon/xpath-checker/ 安装XPath Chec ...
- hdu 5661 Claris and XOR
Claris and XOR Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- Maltab命令(随用随更新)
1.eval() ------ 将括号内的字符串视为语句并运行 比如eval('y1=sin(2)')和语句y1=sin(2)等价 多在循环中使用,可以对多个名字有规则的变量或文件进行操作,比如 fo ...
- HTML5实战1
第一章 1.搭建环境,wamp 2.检查浏览器是否支持html5 ,是否支持新标签<canvas></canvas> 3.简单高效,少用id,多用标签. 4.使用css3美化样 ...
- Link To Sql简单
Linq及其扩展 Linq是一种数据查询语言(它能够从多种数据源中查询数据). 现在基于Linq的扩展有: Linq To Object:主要是从内存对象中查询数据 Linq To Sql:主要是从M ...