//欧拉回路

解法:首先判断欧拉回路存在性: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. jacob 操作word转pdf

    项目需要对上传的word及pdf进行在线预览,因基于jquery的pdf插件,很方面实现在线预览,而word实现在线预览费劲不少,于是想到在进行上传处理时,直接将word转成pdf,在预览时直接预览p ...

  2. [Django 1.5] Windows + Apache + wsgi配置

    基本步骤 下载安装Apache http://httpd.apache.org/download.cgi. 下载安装modwsgi 模块http://code.google.com/p/modwsgi ...

  3. mysql-bin.000001文件的来源及处理方法

    用ports安装了mysql以后,过一段时间发现/var空间不足了,查一下,会发现是mysql-bin.000001.mysql-bin.000002等文件占用了空间,那么这些文件是干吗的?这是数据库 ...

  4. 滴滴专车司机苹果手机ios客户端可以下载了

    申请过滴滴专车司机的朋友都知道,滴滴专车就是滴滴打车的司机端,但是只有安卓的,一直没有苹果IOS的版本,很多申请通过审核的车主恼于没有IOS版本而暂无法使用滴滴专车司机客户端,也就意味着不能上线接单. ...

  5. Hibernate 、多表关联映射-组件关联映射(component)

    组件关联映射可以将一些简小的数据与主题放在一个表中,例如firstName 和LastName这两个结合在一起可以组成一个名字,但是再分别将这两个再建一个表就不太合适了,这个时候可以用到组件关联映射: ...

  6. ios 常用宏(copy)

    分享一下我现在用的 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 ...

  7. strcpy()、memcpy()、memmove()、memset()的内部实现

    一直想知道 strcpy().memcpy().memmove().memset()的内部实现 strcpy(), 字符串拷贝. char *strcpy(char *strDest, const c ...

  8. Neutron数据库同步错误 NotImplementedError: No support for ALTER of constraints in SQLite dialect

    错误信息 root@controller:~# su -s /bin/sh -c "neutron-db-manage --config-file /etc/neutron/neutron. ...

  9. hadoop记录topk

    lk@lk-virtual-machine:~$ cd hadoop-1.0.1 lk@lk-virtual-machine:~/hadoop-1.0.1$ ./bin dfs -mkdir inpu ...

  10. ACM/ICPC2014鞍山现场赛E hdu5074Hatsune Miku

    题目链接:pid=5074">http://acm.hdu.edu.cn/showproblem.php?pid=5074 题意: 给定一个m*m的矩阵mp.然后给定一个长度为n的序列 ...