/*
题意:打印欧拉回路!
思路:开始时不明白,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的更多相关文章

  1. HDU5730 Shell Necklace(DP + CDQ分治 + FFT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of ...

  2. 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 ...

  3. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

  4. HDU 3874 Necklace (树状数组 | 线段树 的离线处理)

    Necklace Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  5. USACO section1.1 Broken Necklace

    /* ID: vincent63 LANG: C TASK: beads */ #include <stdio.h> #include<stdlib.h> #include&l ...

  6. [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链

    [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链 试题描述 欢乐岛上众多新奇的游乐项目让小可可他们玩的非常开心.现在他们正在玩比赛串项链的游戏,谁串的最快就能得到 ...

  7. POJ 1286 Necklace of Beads(Polya原理)

    Description Beads of red, blue or green colors are connected together into a circular necklace of n ...

  8. Accepted Necklace

    Accepted Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...

  9. hdu 2660 Accepted Necklace

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2660 Accepted Necklace Description I have N precious ...

随机推荐

  1. 网页中插入FLASH(swf文件)的html代码

    一.简单插入flash图像<embed src="你的flash地址.swf"width="300" height="220"> ...

  2. erlang日志功能。

    用cowboy这个库,没有日志功能,所以研究了otp提供的日志功能. 1.启动SASL的方式 erl –boot start_sasl 默认配置文件下启动SASL, {env, [{sasl_erro ...

  3. JavaScript对异常的处理

    JavaScript提供了一套异常处理机制.当查出事故时,你的程序应该抛出一个异常: var add=function(a,b){ if(typeof a !== 'number' || typeof ...

  4. HTML5实战1

    第一章 1.搭建环境,wamp 2.检查浏览器是否支持html5 ,是否支持新标签<canvas></canvas> 3.简单高效,少用id,多用标签. 4.使用css3美化样 ...

  5. Android JIT实时编译器的设置

    在Android  JIT实时编译是在Android 2.2之后才引入的,JIT编译器可以显著的提高机器的性能,经过测试,android 2.2的性能较android 2.1提高了 2-5倍.JIT提 ...

  6. sum() 函数

    sum()的参数是一个list 例如: sum([1,2,3])

  7. 《理解 ES6》阅读整理:函数(Functions)(七)Block-Level Functions

    块级函数(Block-Level Functions) 在ES3及以前,在块内声明一个函数会报语法错误,但是所有的浏览器都支持块级函数.不幸的是,每个浏览器在支持块级函数方面都有一些细微的不同的行为. ...

  8. spring 装配核心笔记

    (1)自动装配 开启ComponentScan(自动扫描), 通过在类使用注解@Component(默认bean id为类名第一个字符小写), 使用@Autowired实现属性,构造函数,成员函数的自 ...

  9. Oracle中的Temporary tablespace的作用

    临时表空间主要用途是在数据库进行排序运算[如创建索引.order by及group by.distinct.union/intersect/minus/.sort-merge及join.analyze ...

  10. Backbone源码解析(二):Model(模型)模块

    Model(模型)模块在bk框架中的作用主要是存储处理数据,它对外和对内都有很多操作数据的接口和方法.它与视图(Views)模块精密联系着,通过set函数改变数据结构从而改变视图界面的变化.下面我们来 ...