//欧拉回路

解法:首先判断欧拉回路存在性:1、连通 2、没有出度入度相差大于1的点 3、如果有出度入度相差等于1的点那么必须有两个,一个出度大于入度作为起点,一个入度大于出度作为终点。

在确定了起点后,用dfs法找欧拉回路。

关于dfs找欧拉回路:其实就是欧拉回路存在的充要性定理的证明,先走到底(最后走到的一定是终点,如果重点起点固定的话),然后再递归回来找圈,因为图连通,后来找到的圈也可以加到里面。

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<string> using namespace std; int e[][];
int n;
int din[],dout[];
char s[];
int maxLen;
int start;
int num=;
char ans[];
bool flag=false; int getid(char c){
if (c<='z' && c>='a') return c-'a';
if (c<='Z' && c>='A') return c-'A'+;
return c-''+;
} int getidfrom(char* s){
return getid(s[])*+getid(s[]);
} int getidto(char* s){
return getid(s[])*+getid(s[]);
} char getch(int x){
if (x< && x>=) return 'a'+x;
if (x< && x>=) return 'A'+x-;
return x+''-;
} void dfs(int now){
for (int i=;i<=maxLen;i++){
if (e[now][i]>){
e[now][i]--;
dfs(i);
}
}
if (!flag){
flag=true;
ans[num++]=getch(now%);
}
ans[num++]=getch(now/);
} int main(){
scanf("%d",&n);
memset(e,,sizeof(e));
memset(din,,sizeof(din));
memset(dout,,sizeof(dout));
maxLen=getid('')*+getid('');
for (int i=;i<n;i++){
scanf("%s",s);
int from=getidfrom(s);
int to=getidto(s);
dout[from]++;
din[to]++;
e[from][to]++;
}
int cnt1=;
int cnt2=;
start=-;
for (int i=;i<=maxLen;i++){
if (start==- && (din[i]!= || dout[i]!=)) start=i;
if (abs(din[i]-dout[i])>){
printf("NO\n");
return ;
}
if (din[i]>dout[i]) cnt1++;
if (din[i]<dout[i]){
cnt2++;
start=i;
}
}
if (!((cnt1== && cnt2==)||(cnt1== && cnt2==))){
printf("NO\n");
return ;
}
dfs(start);
if (num!=n+){
printf("NO\n");
return ;
}
printf("YES\n");
for (int i=num-;i>=;i--){
printf("%c",ans[i]);
}
printf("\n");
return ;
}
/*
4
baa
caa
aax
aay 10
Aa3
a3X
3XX
XXy
Xy1
y12
123
234
345
456 5
123
234
345
456
567 3
123
231
312
*/

cdoj 韩爷的情书 有向图 欧拉路径的更多相关文章

  1. UESTC_韩爷的情书 2015 UESTC Training for Graph Theory<Problem H>

    H - 韩爷的情书 Time Limit: 6000/2000MS (Java/Others)     Memory Limit: 262144/262144KB (Java/Others) Subm ...

  2. UESTC_韩爷的梦 2015 UESTC Training for Search Algorithm & String<Problem N>

    N - 韩爷的梦 Time Limit: 200/100MS (Java/Others)     Memory Limit: 1300/1300KB (Java/Others) Submit Stat ...

  3. 2015 UESTC 搜索专题N题 韩爷的梦 hash

    韩爷的梦 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descrip ...

  4. cdoj 1092 韩爷的梦

    http://acm.uestc.edu.cn/#/problem/show/1092 题意:略 思路: 做的第一道字符串hash的题,真是菜啊,还是看了几篇题解才会做的.字符串hash感觉就是函数的 ...

  5. UVa 10129 Play on Words(有向图欧拉路径)

    Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to s ...

  6. POJ 1386 Play on Words (有向图欧拉路径判定)

    Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8768   Accepted: 3065 Des ...

  7. POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8756   Accepted: 2306 Descript ...

  8. hiho一下 第五十一周(有向图欧拉路径)51

    //////////////////////////////////////////////////////////////////////////////////////////////////// ...

  9. hdu1116有向图判断欧拉通路判断

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

随机推荐

  1. yacc和lex在ubuntu上安装

    在编译boa webserver源码的时候./configure的过程中遇到找不到yacc的提示. 采用以下失败: sudo apt-get install yacc lex   应替换为: sudo ...

  2. 动态加载Layout 与 论Activity、 Window、View的关系

    1)动态加载Layout的代码是 getWindow().setContentView(LayoutInflater.from(this).inflate(R.layout.main, null)); ...

  3. Linq to sql 操作

    1.往数据库添加数据 NorthwindDataContext abc = new NorthwindDataContext(); abc.Log = Console.Out; User a = ne ...

  4. Http网络请求

    前提说明: 1.Str.Empty 其实就是 string.Empty 2.@object.IsNull() 的IsNull() 是判断“是否等于null”的扩展方法,等同于  @object==nu ...

  5. [问题解决] /home目录占用率100%

    今天发现一个比较奇怪的现象,linux系统下一个目录挂在存储下,df -Th 显示该目录使用率100%, du 该目录显示只用了2%, 后来发现是由于进程占用了被删掉的文件空间导致.举例如下: [ro ...

  6. git创建与合并分支

    创建与合并分支 在版本回退里,你已经知道,每次提交,Git都把它们串成一条时间线,这条时间线就是一个分支.截止到目前,只有一条时间线,在Git里,这个分支叫主分 支,即master分支.HEAD严格来 ...

  7. Jupyter Notebook通过latex输出pdf

    主要步骤 1.将ipynb编译成tex ipython nbconvert --to latex Example.ipynb 2. 修改tex,增加中文支持 在\documentclass{artic ...

  8. Struts2 多文件上传

    Struts2多文件上传只需要将 单文件上传中的File变成File[]  即可,上篇文章:单文件上传 <form action="${pageContext.request.cont ...

  9. 在win7下php查询数据库, 连接被重置

    解决方法一 将 php5 目录下的libmysql.dll拷贝至 windows/system32和apache/bin下 解决方法二 在apache/conf/httpd.conf文件中添加 Loa ...

  10. 如何将你牛逼的iOS代码分享到CocoaPod(转)

    为了让读者一目了然,现在就从新建工程到最后提交podspec,简单粗暴的讲一通.Cocoapods不用解释太多,一句话...它是目前iOS开发中最广为使用的第三方库依赖管理工具. 下面开始讲创建pod ...