cdoj 韩爷的情书 有向图 欧拉路径
//欧拉回路
解法:首先判断欧拉回路存在性: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 韩爷的情书 有向图 欧拉路径的更多相关文章
- UESTC_韩爷的情书 2015 UESTC Training for Graph Theory<Problem H>
H - 韩爷的情书 Time Limit: 6000/2000MS (Java/Others) Memory Limit: 262144/262144KB (Java/Others) Subm ...
- 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 ...
- 2015 UESTC 搜索专题N题 韩爷的梦 hash
韩爷的梦 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descrip ...
- cdoj 1092 韩爷的梦
http://acm.uestc.edu.cn/#/problem/show/1092 题意:略 思路: 做的第一道字符串hash的题,真是菜啊,还是看了几篇题解才会做的.字符串hash感觉就是函数的 ...
- UVa 10129 Play on Words(有向图欧拉路径)
Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to s ...
- POJ 1386 Play on Words (有向图欧拉路径判定)
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8768 Accepted: 3065 Des ...
- POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8756 Accepted: 2306 Descript ...
- hiho一下 第五十一周(有向图欧拉路径)51
//////////////////////////////////////////////////////////////////////////////////////////////////// ...
- hdu1116有向图判断欧拉通路判断
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
随机推荐
- Apache-Tomcat 和 Apache-Maven配置
1.1.下载安装文件 官网对应版本下载,例:apache-tomcat-8.0.35-windows-x64.zip 1.2.指定对应的安装目录: 例:D:\JavaSoft\apache-tomca ...
- PS切图保存后的背景图为透明
1.若想PS切图保存后的背景图为透明,那么则需要在如下图中所示的修改即可,切图后[文件]——>[存储为web格式]——>[PNG-24]: 2.要想在css中的背景图片为相通,则先剪切一个 ...
- listview优化
http://www.2cto.com/kf/201108/99928.html 项目用到ListView,由于要用到ImageView,图片源不是在资源里面的,没法使用资源ID,因此无法直接使用Si ...
- vs2008调试 Release(链接器来生成调试信息)
VS2008 Release 修改配置: 1.项目——>属性——>C/ C++ ——> 常规 ——>调试信息格式——>用于“编辑并继续”的程序数据库(/ZI) 2.项目— ...
- CSS左中右布局,规范案例
html部分 <body> <form id="form1" runat="server"> <div id="wrap ...
- unity3d银联支付出现闪退
在接入银联SDK的unity3d的游戏中,点击银联支付会出现游戏总是出现闪退现象 ,后来网上有资料说 打开银联的jar包,将里面的res/drawable目录下的文件,拷贝到您的工程的drawable ...
- javascript第七课js函数
function add() { } 上面就是js中的方法,js中的方法与c#中的方法不同的是不需要写返回值类型 function add(num1,num2) { return num1+num2 ...
- IEnumerable和IEnumerator 详解 【转】
初学C#的时候,老是被IEnumerable.IEnumerator.ICollection等这样的接口弄的糊里糊涂,我觉得有必要切底的弄清楚IEnumerable和IEnumerator的本质. 下 ...
- python 网页爬虫+保存图片+多线程+网络代理
今天,又算是浪费了一天了.python爬虫,之前写过简单的版本,那个时候还不懂原理,现在算是收尾吧. 以前对网页爬虫不了解,感觉非常神奇,但是解开这面面纱,似乎里面的原理并不是很难掌握.首先,明白一个 ...
- NHibernate 3.3
今天试了一下NHibernate 3.3比之前的版本简单,只需要引入两个dll,这两个dll分别是:Iesi.Collections.dll和NHibernate.dll 通过 http://nhfo ...