poj 2337 欧拉回路输出最小字典序路径 ***
把26个小写字母当成点,每个单词就是一条边。
然后就是求欧拉路径。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-;
typedef long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=;
int n,m,tt;
string s[MAXN];
int ans[MAXN],in[],out[];
struct Edge
{
int to,next;
int id;
bool vis;
}edge[MAXN];
int head[],tot;
void init()
{
tot = ;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int id)
{
edge[tot].to=v;
edge[tot].next=head[u];
edge[tot].id=id;
edge[tot].vis=;
head[u]=tot++;
}
int cnt;
bool dfs(int u)
{
for(int i=head[u];i!=-;i=edge[i].next)
{
if(!edge[i].vis)
{
edge[i].vis=;
dfs(edge[i].to);
ans[cnt++]=edge[i].id;
}
}
}
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
scanf("%d",&tt);
while(tt--)
{
cnt=;
init();
scanf("%d",&n);
for(i=;i<n;i++) cin>>s[i];
sort(s,s+n);
cl(in),cl(out);
int u,v,st=;
for(i=n-;i>=;i--) //注意前向星是后加入的边先遍历
{
u=s[i][]-'a';
v=s[i][s[i].length()-]-'a';
addedge(u,v,i);
out[u]++;
in[v]++;
if(st>u) st=u;
if(st>v) st=v;
}
int sc1=,sc2=;
for(i=;i<;i++)
{
if(out[i]-in[i]==) //只能从这出发
{
sc1++;
st=i;
}
else if(out[i]-in[i]==-) //只能从这出发
{
sc2++;
}
else if(out[i]-in[i]!=) sc1=; //不是欧拉回路
}
if(!((sc1==&&sc2==)||(sc1==&&sc2==)))
{
printf("***\n");
continue;
}
dfs(st);
if(cnt!=n)//判断是否连通
{
printf("***\n");
continue;
}
for(int i=cnt-;i>=;i--)
{
cout<<s[ans[i]];
if(i>)printf(".");
else printf("\n");
}
}
}
poj 2337 欧拉回路输出最小字典序路径 ***的更多相关文章
- ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)
题目链接: https://vjudge.net/problem/ZOJ-1456 These are N cities in Spring country. Between each pair of ...
- POJ 2337 Catenyms(欧拉回(通)路:路径输出+最小字典序)
题目链接:http://poj.org/problem?id=2337 题目大意:给你n个字符串,只有字符串首和尾相同才能连接起来.请你以最小字典序输出连接好的单词. 解题思路:跟POJ1386一个意 ...
- poj 1041(欧拉回路+输出字典序最小路径)
题目链接:http://poj.org/problem?id=1041 思路:懒得写了,直接copy吧:对于一个图可以从一个顶点沿着边走下去,每个边只走一次,所有的边都经过后回到原点的路.一个无向图存 ...
- POJ 2337 欧拉回路
题意: 如果给出的单词能够首尾相接,请按字典序输出单词,中间要加'.' 否则输出三个"*". 思路: 欧拉回路 记得按字典序排序哦~ 加边的时候要倒着加.(邻接表遍历的时候是反着的 ...
- poj 2337 有向图输出欧拉路径
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10186 Accepted: 2650 Descrip ...
- HDU 1814 Peaceful Commission(2-sat 模板题输出最小字典序解决方式)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 Problem Description The Public Peace Commission ...
- c4 L3-001 找零钱 (简单01背包-输出最小字典序解(用vector保存当前最优解))
#include <iostream> #include <algorithm> #include <vector> #include <cstdio> ...
- Catenyms POJ - 2337(单词+字典序输出路径)
题意: 就是给出几个单词 看能否组成欧拉回路或路径 当然还是让输出组成的最小字典序的路 解析: 还是把首尾字母看成点 把单词看成边 记录边就好了 这题让我对fleury输出最小字典序又加深了一些 ...
- TZOJ 5110 Pollutant Control(边数最少最小割最小字典序输出)
描述 It's your first day in Quality Control at Merry Milk Makers, and already there's been a catastrop ...
随机推荐
- c++写入txt文件
简单方式: #include "stdafx.h" #include <iostream> #include <iomanip> #include < ...
- PHP 知识点链接
PHP 1.PHP中的危险函数全解析 http://www.php100.com/html/webkaifa/PHP/PHPyingyong/2009/0216/333 ...
- 同级兄弟元素之间的CSS控制
为了实现这个导航效果:选中菜单中某一项,隐藏掉自身的左边背景线条,同时让他的下一个兄弟元素也隐藏掉线条. 有一种选择器就叫兄弟元素选择器,分为临近兄弟和普通兄弟. 临近兄弟:用 + 表示,只匹配该元素 ...
- javascript 数组去重
2015年5月15日 20:17:05 星期五 原理: .......(说不清楚, 自己看代码吧, 很简单.....) //去重 var hash_already_input = {}; for (v ...
- 在linux环境编译boost
1.在boost官网:http://www.boost.org/下载相应版本的boost 2.解压boost到相应目录,在boost跟目录下有b2可执行程序,可以通过输入命令“/b2 --help”, ...
- 添加或修改ssh服务的端口
通常ssh远程登录的默认端口是22,这个端口一般是可以更改或者添加的,配置文件位置在:/etc/ssh/sshd_config通过编辑文件可以修改sshd服务的相关配置,以下新增端口2223,即除了2 ...
- JAVA thread0.interrupt()方法
interrupt()只是改变中断状态而已,interrupt()不会中断一个正在运行的线程.这一方法实际上完成的是,给受阻塞的线程抛出一个中断信号,这样受阻线程就得以退出阻塞的状态. 更确切的说,如 ...
- bootstrap 重写JS的alert、comfirm函数
原理是使用bootstrap的Modal插件实现. 一.在前端模板合适的地方,加入Modal展现div元素. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
- Cuckoo for Hashing_双哈希表
问题 B: Cuckoo for Hashing 时间限制: 1 Sec 内存限制: 64 MB提交: 24 解决: 12[提交][状态][讨论版] 题目描述 An integer hash ta ...
- dhtmlxTree介绍(转载)
dhtmlxTree 是树菜单,允许我们快速开发界面优美,基于Ajax的javascript库. 她允许在线编辑,拖拽,三种状态(全选.不选.半选),复选框等模式.同时在加载大数据量的时候,仍然 可以 ...