poj 2337 有向图输出欧拉路径
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 10186 | Accepted: 2650 |
Description
dog.gopher
gopher.rat
rat.tiger
aloha.aloha
arachnid.dog
A compound catenym is a sequence of three or more words separated by periods such that each adjacent pair of words forms a catenym. For example,
aloha.aloha.arachnid.dog.gopher.rat.tiger
Given a dictionary of lower case words, you are to find a compound catenym that contains each of the words exactly once.
Input
Output
Sample Input
2
6
aloha
arachnid
dog
gopher
rat
tiger
3
oak
maple
elm
Sample Output
aloha.arachnid.dog.gopher.rat.tiger
*** 先判断是否存在欧拉路径,然后再按照字典序输出欧拉路径,想写个非递归的深搜,可惜失败了
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<climits>
#define MAXE 1010
#define MAXP 28
using namespace std;
struct Edge
{
int s,t,next;
char str[];
}edge[MAXE];
int head[MAXE];
int degree[MAXP];
int fa[MAXP];
int stack[MAXE];
bool used[MAXP];
bool sign[MAXE];
bool cur[MAXE][MAXE];
int start;
int n;
int top;
bool cmp(Edge a,Edge b)
{
return strcmp(a.str,b.str)>;
}
void add(int s,int t,int ent)
{
edge[ent].s=s;
edge[ent].t=t;
edge[ent].next=head[s];
head[s]=ent;
}
int find(int x)
{
int temp=x,i;
while(fa[x]!=x)
x=fa[x];
while(fa[temp]!=x)
{
i=fa[temp];
fa[temp]=x;
temp=i;
}
return x;
}
bool oula()
{
int temp=,temp2=;
start=edge[n].s;
for(int i=;i<=;i++)
{
if(used[i])
{
if(fa[i]==i)temp++;
if(degree[i])
{
if(degree[i]>||degree[i]<-)return false;
if(degree[i]==-)start=i;
temp2++;
}
}
}
if(temp!=)return false;
if(temp2&&temp2!=)return false;
return true;
}
void dfs(int s)
{
for(int i=head[s];i!=-;i=edge[i].next)
{
if(!sign[i])
{
sign[i]=true;
dfs(edge[i].t);
stack[++top]=i;
}
}
/*while(1)
{
if(top==n)break;
int temp=head[s];
for(int i=head[s];i!=-1;temp=i=edge[i].next)
{
if(!sign[i]&&!cur[stack[top]][i])break;
}
if(temp==-1)
{
cur[stack[top-1]][stack[top]]=true;
sign[stack[top]]=false;
sign[stack[top-1]]=false;
s=edge[stack[--top]].s;
}
else
{
stack[++top]=temp;
s=edge[temp].t;
sign[temp]=true;
}
}*/
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
memset(head,-,sizeof(head));
memset(sign,false,sizeof(sign));
memset(used,false,sizeof(used));
memset(cur,false,sizeof(cur));
memset(degree,,sizeof(degree));
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%s",edge[i].str);
sort(edge+,edge++n,cmp);
for(int i=;i<=;i++)
fa[i]=i;
for(int i=;i<=n;i++)
{
int s=edge[i].str[]-'a'+,t=edge[i].str[strlen(edge[i].str)-]-'a'+;
add(s,t,i);
fa[find(t)]=find(s);
degree[s]--;
degree[t]++;
used[s]=true;
used[t]=true;
}
if(!oula())
{
printf("***\n");
}
else
{
top=;
dfs(start);
/*for(int i=1;i<=top-1;i++)
printf("%s.",edge[stack[i]].str);
printf("%s\n",edge[stack[top]].str);*/
for(int i=top;i>=;i--)
printf("%s.",edge[stack[i]].str);
printf("%s\n",edge[stack[]].str);
}
}
return ;
}
poj 2337 有向图输出欧拉路径的更多相关文章
- poj 2337 Catenyms 【欧拉路径】
题目链接:http://poj.org/problem?id=2337 题意:给定一些单词,假设一个单词的尾字母与还有一个的首字母同样则能够连接.问能否够每一个单词用一次,将全部单词连接,能够则输出字 ...
- poj 2337 欧拉回路输出最小字典序路径 ***
把26个小写字母当成点,每个单词就是一条边. 然后就是求欧拉路径. #include<cstdio> #include<iostream> #include<algori ...
- POJ 2337 输出欧拉路径
太无语了. 这道题做了一整天. 主要还是我太弱了. 以后这个就当输出欧拉路径的模版吧. 题目中的输出字典序最小我有点搞不清楚,看了别人是这么写的.但是我发现我过不了后面DISCUSS里面的数据. 题意 ...
- POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8756 Accepted: 2306 Descript ...
- POJ 2337 Catenyms(有向欧拉图:输出欧拉路径)
题目链接>>>>>> 题目大意: 给出一些字符串,问能否将这些字符串 按照 词语接龙,首尾相接 的规则 使得每个字符串出现一次 如果可以 按字典序输出这个字符串 ...
- POJ 2337 Catenyms(欧拉回(通)路:路径输出+最小字典序)
题目链接:http://poj.org/problem?id=2337 题目大意:给你n个字符串,只有字符串首和尾相同才能连接起来.请你以最小字典序输出连接好的单词. 解题思路:跟POJ1386一个意 ...
- POJ 2337 Catenyms(有向图的欧拉通路)
题意:给n个字符串(3<=n<=1000),当字符串str[i]的尾字符与str[j]的首字符一样时,可用dot连接.判断用所有字符串一次且仅一次,连接成一串.若可以,输出答案的最小字典序 ...
- poj 2337(单向欧拉路的判断以及输出)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11648 Accepted: 3036 Descrip ...
- Poj 2337 Catenyms(有向图DFS求欧拉通路)
题意: 给定n个单词, 问是否存在一条欧拉通路(如acm,matal,lack), 如果存在, 输出字典序最小的一条. 分析: 这题可以看作http://www.cnblogs.com/Jadon97 ...
随机推荐
- sql语句与数据库2
增加数据 insert into wyx(xh,nl,xb,sfzh,zcrq)values(0422,28,男,210623198711111111,2016-8-19); 删除数据 delete ...
- gdb使用心得
启用调试gdb gdb 路径到prog_1dray 然后就直接进去gdb了 进去后输入r *.par 参数文件就行了 暂时学到这,用到这!日后再学习更新
- Firebug在Firefox DevTools 中复活
英文:Firefox,编译:开源中国 链接:www.oschina.net/news/80230/firebug-lives-on-in-firefox-devtools 技术最前线转注:2016年1 ...
- 一个很奇怪的重复链接lib的问题
早上在调一个程序的时候感觉非常奇怪,就是数据在初始化的时候会失败,后来发现是获取一个数据的时候出错了 假设我们又一个config.lib,sql.dll和main.exe 因为数据库在打开数据库的时候 ...
- JsonResult序列化并保存json文件 以及对json文件读取反序列
项目中我们经常遇到一些经常访问的接口,并且更新及时度不是特别高,那么我们可以利用文件来做一些数据请求的缓存. 这里以微信公众号获取粉丝用户列表为例,我们把微信公众号查到的用户先缓存在文件中,这样在翻页 ...
- HTML 全局属性_02
New : HTML5 新属性. 属性 描述 accesskey 设置访问元素的键盘快捷键. class 规定元素的类名(classname) contenteditableNew 规定是否可编辑元素 ...
- JSTL标签库中<c:choose></c:choose>不能放JSP页面<!-- -->注释
笔者最近在使用JSTL标签库的<c:choose>标签时候,发现在该标签体中加了JSP的<!-- -->注释时,总是会显示报错信息.错误的信息如下: <span styl ...
- ADF_Starting系列8_使用EJB/JPA/JSF通过ADF构建Web应用程序之扩展UI Method
2013-05-01 Created By BaoXinjian
- 堆外内存操作类ByteBuffer
本篇主要讲解如何使用直接内存(堆外内存),并按照下面的步骤进行说明: 1 相关背景-->读写操作-->关键属性-->读写实践-->扩展-->参考说明 希望对想使用直接内存 ...
- 【Python与机器学习】:利用Keras进行多类分类
多类分类问题本质上可以分解为多个二分类问题,而解决二分类问题的方法有很多.这里我们利用Keras机器学习框架中的ANN(artificial neural network)来解决多分类问题.这里我们采 ...