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 ...
随机推荐
- js/jquery获取当前页面URL地址并判断URL字符串中是否包含某个具体值
js/jquery获取当前页面URL地址并判断URL字符串中是否包含某个具体值本文介绍jquery/js获取当前页面url地址的方法,在jquery与js中获取当前页面url方法是一样的,因为jque ...
- jquerymobile listview 局部刷新
function onSuccess(data, status) { data = $.trim(data); // alert(data); // return; if (data) { $('#l ...
- EditText 空指针问题
今天在Android中碰到了这样一个问题,其实应该很少人会碰到,因为只有像我这种奇葩才会犯这种错误. 但既然解决了,我就想在这里跟大家分享一下,毕竟它困扰了我一个白天啊...不多说了,看下面... 其 ...
- MySQL----information-schema数据库相关权限的说明。
MySQL中的information_schema数据库比较特别有如下几个要注意的地方. 1.就算是一个新创建的用户,也就是说这个用户只有一个usage权限.它都可以查看informatoin_sch ...
- Codeforces 339E
#include <cstdio> #include <algorithm> #include <cmath> #include <cstring> # ...
- ceph理论及部署配置实践
prefaces: ceph installation(quick)1,preflight(ins ceph-deploy repo tools)2,ceph storage cluster quic ...
- MyGui笔记(1)建立第一个工程
记录下学习 MyGui的一些笔记,从建立第一个工程开始. 步骤: 1.右键MYGUI解决方案,添加→新建项目,选择“Win32 项目”,名称为:TestHello.下一步,勾选“空项目”. 2.设置工 ...
- Hibernate 、继承关联映射
一个继承树一张表(subclass) 一个继承树一张表是指将一个类和他下面的所有派生类的所有的属性都放在一张表中,举例有Employee这个实体,在他下面分别派生了两个类skill和sell,通过Hi ...
- iOS多线程GCD(转)
原文:http://www.cnblogs.com/pure/archive/2013/03/31/2977420.html Grand Central Dispatch (GCD)是Apple开发的 ...
- sqlplus handbook
1.直接敲sqlplus并回车就是启动SQL*PLUS,输入user及password将使用户登陆到缺省的数据库. 请输入用户名: 2.sqlplus user/password@SERVICE_NA ...