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 ...
随机推荐
- A Brief Introduction to Markovs Chains
本文译自A Brief Introduction to Markovs Chains 译者按: 前面一篇文章讲的是蒙特卡洛积分,也就是通过生成符合特定分布的随机变量来近似计算积分值,例如: \(E = ...
- Monte Carlo Approximations
准备总结几篇关于 Markov Chain Monte Carlo 的笔记. 本系列笔记主要译自A Gentle Introduction to Markov Chain Monte Carlo (M ...
- sql 查询效率
1. SQL优化的原则是:将一次操作需要读取的BLOCK数减到最低,即在最短的时间达到最大的数据吞吐量.调整不良SQL通常可以从以下几点切入: 检查不良的SQL,考虑其写法是否还有可优化内容 检查子查 ...
- Android——数据存储(课堂代码整理:SharedPreferences存储和手机内部文件存储)
layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- android 4.4.2 开发环境
1.设置环境变量 set "JAVA_HOME=C:\Program Files\Java\jdk1.7.0_75" set "ANT_HOME=D:\tools\and ...
- Ubuntu 16.10 在 VMware 上无法安装的解决办法
参考:http://askubuntu.com/questions/840822/ubuntu-16-10-doesnt-work-in-virtual-machine-vmware 1- Edit ...
- Spring Framework----定时任务的执行和调度
1. 简介 spring framework 为任务的异步执行和调度提供了抽象接口分别是:TaskExecutor 和 TaskScheduler,spring 对这些接口的进一步实现支持线程池或者将 ...
- c#读取快递100查询返回的JSON信息
{"message":"ok","nu":"1105016801203","companytype" ...
- 51nod 最近刷题 简要题解
51nod 1564 由于数据是随机的,可以证明,对于每一个数,向左或右找比它小的数,长度是logn级别的 考虑枚举最大值 注意,对于每一个最大值,如果直接用2个循环枚举左右端点的话,理论是lognl ...
- Android String操作
android String.valueOf(ch).getBytes("GBK") --------------------------------------------- S ...