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 ...
随机推荐
- css实现背景颜色线性渐变
http://www.qttc.net/201304316.html http://www.ruanyifeng.com/blog/2008/05/css_background_image_posit ...
- 迷宫问题_BFS_挑战程序设计竞赛p34
给定一个N*M的迷宫,求从起点到终点的最小步数. N,M<100: 输入: 10 10#S######.#......#..#.#.##.##.#.#........##.##.####.... ...
- nyoj138 找球号(二)_离散化
找球号(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中,每个球上都有一个整数编号i(0<=i< ...
- 安装/移除Windows服务
在工作中,涉及到相关Windows的开发时,常常要安装.移除Windows服务程序.一般地,可通过cmd命令行操作来完成~ cd 服务程序所在目录 *.exe -i // 安装服务 *.exe -s ...
- Spring配置数据源
Spring在第三方依赖包中包含了两个数据源的实现类包,其一是Apache的DBCP,其二是 C3P0.可以在Spring配置文件中利用这两者中任何一个配置数据源. DBCP数据源 DBCP类包位于 ...
- jquery格式化时间
使用方法: new Date().format("yyyy-MM-dd hh:mm:ss"); 格式: Date.prototype.format = function (form ...
- Windows update 失败的解决方案
由于刚刚装了新的系统,结果在电脑用了刚刚一周之后,我打开了自动更新,结果电脑就一直处于更新状态中.期初我以为是更新比较慢,等等可能就结束了,结果等了足足一晚上,到了第二天,电脑显示是:配置更新失败,正 ...
- HDU 5995 Kblack loves flag ---BestCoder Round #90
题目链接 用两个布尔数组分别维护每个行/列是否被插过旗帜,最后枚举每一行.列统计答案即可.空间复杂度O(n+m),时间复杂度O(n+m+k). #include <cstdio> #inc ...
- Struts2防止表单重复提交
1.说明 系统拦截器的应用. 表单重复提交:当使用请求转化进行跳转的时候,存在着表单重复提交的问题. 2.在表单中加入s:token 如果页面加入了struts2的标签,页面的请求必须进入struts ...
- ASINetworkQueues(经典2)
ASINetworkQueues, 它的delegate提供更为丰富的功能 提 供的更多的回调方法如下: a,requestDidStartSelector,请求发起时会调此方法,你可以在此方法中跟据 ...