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 ...
随机推荐
- mysql explain 命令简解
参考官方文档地址: http://dev.mysql.com/doc/refman/5.7/en/explain.html 为什么用explain . 如果你的页面返回结果很慢,你就需要使用expla ...
- 嵌入式平台组件白盒测试gcov、lcov和genhtml 使用指导
在嵌入式平台上使用了gtest白盒测试工具,覆盖了被测函数,但是不知道自己测试的效果如何,测试行覆盖率.函数覆盖率,分支覆盖率的数据. 便开始研究gcov这个代码覆盖率工具能否使用,来检查白盒测试的效 ...
- MYSQL 部分事务
MYSQL 中通过 savepoint 的方式来实现只提交事务的一部分. step 1 : savepoint savepoint_name;. 做标记 step 2 :rollbak to save ...
- Hibernate实现分页
转自:http://blog.csdn.net/huqiub/article/details/4329541 分页在任何系统中都是非常头疼的事情,有的数据库在语法上支持分页,而有的数据库则需要使用可滚 ...
- 从ASP.NET传递参数给水晶报表
原文 http://www.cnblogs.com/insus/p/3281114.html 上次Insus.NET有简单写了一篇文章<Visual Studio 2012使用水晶报表Cryst ...
- Java Servlet的配置文件web.xml配置内容和具体含义
文件名:“SimpleServlet.java” package cn.mldn.lxh.servlet ;//定义包 import java.io.* ; // HttpServlet属于javax ...
- Centos6.5升级gcc for qt5.3.1
1.升级GCC CentOS6.5内置的GCC版本为4.4,而Qt5.2.1则需要4.8.2的支持(支持C++ 11特性),因此,必须先升级GCC wget http://ftp.tsukuba.wi ...
- iOS 开发常用设置
1. iOS设置app应用程序文件共享 设置流程 xcode 打开项目----在 info.plist 文件,添加 UIFileSharingEnabled 并设置属性为 YES, 在app内部,将您 ...
- 你必须知道的 34 个简单实用的 Ubuntu 快捷键
ubuntu常用的快捷键: 1. Ctrl + W: 关闭当前 Nautilus 窗口 2. Ctrl+T: 在 Nautilus 打开新的 Tab 3. Ctrl + H: 切换隐藏文件(夹)显 ...
- hdu 3068 最长回文(manachar求最长回文子串)
题目连接:hdu 3068 最长回文 解题思路:通过manachar算法求最长回文子串,如果用遍历的话绝对超时. #include <stdio.h> #include <strin ...