Sea and Sky are the most favorite things of iSea, even when he was a small child.  Suzi once wrote: white dew fly over the river, water and light draw near to the sky. What a wonderful scene it would be, connecting the two charming scenery. But iSea cannot ask help from God, or some other deities in China. The only mean he can use is imagination. 
For example, from sea, he can associate with love, from love, he can see sky in (strange logic, aha? leave him alone, we don't really care how he imagine since he is so weird). In this way, he connects "Sea" and "Sky" in mind, fulfills his goal.  However, he can only solve the puzzle with small number of words, when the connection increases, his brain will come to be a total mess. Now, can you smart guys help him? 
Now iSea gives you some word pairs he can associate, from any one of them to another. He wishes use the maximum word to make an association list, from “sea” to “sky”, of course, no word should appear in the list twice because it would lead to an infinite loop. Your task is to find a list, which contains the maximum word and every neighbor word can be connected in mind. If several solutions exist, find the lexicographically minimum one.  Lexicographical sequence is the order in one dictionary. For example, “cat” is less than “do”, and “do” is less than “dog”.
 

Input

The first line contains a single integer T, indicating the number of test cases.  Each test case begins with an integer N, then N lines follow, each line contains two words can be connected in mind. 
Technical Specification 
1. 1 <= T <= 50  2. 1 <= N <= 100  3. The number of different words and the length of words is no more than sixteen.
 

Output

For each test case, output the case number first, if cannot finish, output “what a pity”. Otherwise, output a word sequence with most words, separated by a blank.
 

Sample Input

3
2
sea love
sky love
7
sea pure
pure air
air white
sky white
pure holy
holy white
sky holy
3
sea blue
sky white
blue green
 

Sample Output

Case 1: sea love sky
Case 2: sea pure air white holy sky
Case 3: what a pity
 
题意: 给出N对字符串,每对字符串可以相连,问能否找到一条从sea到sky的字符串链,每个字符串只能出现一次,如果能,输出
长度最长的,如果有多解,输出字典序最小的。无解输出what a pity
 
解析: 刚开始还以为是个有环找最长路的图论题(然而我并不会写)。。。。。。看了别人题解才知道是深搜+剪枝。
对字符串排序标号,如果没有出现sea或者sky,直接无解,然后并查集一下,如果不在同一个集合,同样无解,然后判断每个点
(起点和终点除外)是否能到达起点和终点,不能的在搜的过程中直接不管,然后就是搜了,搜的过程中更新解,如果最大长度
已经用完了所有的字符串就没必要再搜了(最开始我没管这个超时了。。。。。。后来加了这个剪枝就过了)
 
代码
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=;
int N,id,be,en;
string S1[maxn],S[maxn];
int d[maxn];
int root(int a){ return d[a]==a?a:d[a]=root(d[a]); }
bool G[maxn][maxn];
int GetId(string& s) //找到下标
{
for(int i=;i<=id;i++) if(S[i]==s) return i;
return ;
}
bool input()
{
N*=;
for(int i=;i<=N;i++)
{
cin>>S1[i];
S[i]=S1[i];
}
sort(S+,S+N+); //排个序
be=en=; //起点和终点编号
id=;
for(int i=;i<=N;i++)
{
if(S[i]!=S[id]) S[++id]=S[i]; //去重
if(S[id]=="sea") be=id;
if(S[id]=="sky") en=id;
}
if(!be||!en) return false; //没有sea或者sky
for(int i=;i<maxn;i++) d[i]=i; //并查集
memset(G,false,sizeof(G));
for(int i=;i<N;i+=)
{
int a=GetId(S1[i]); //下标
int b=GetId(S1[i+]);
int ra=root(a);
int rb=root(b);
G[a][b]=G[b][a]=true; //可连
if(ra!=rb) d[rb]=ra; //合并
}
if(root(be)!=root(en)) return false; //不在同一集合
return true;
}
bool vis[maxn],tvis[maxn];
int maxl,temp[maxn],ans[maxn];
bool dfs(int x,int step)
{
vis[x]=true;
temp[step]=x;
if(x==en) //到终点
{
if(step>maxl) //更新解
{
maxl=step;
for(int i=;i<=maxl;i++) ans[i]=temp[i];
}
if(step==id-) return true; //用完所有的
}
for(int i=;i<=id;i++)
if(!vis[i]&&G[x][i])
{
if(dfs(i,step+)) return true;
}
vis[x]=false;
return false;
}
bool Reach(int x,int y)
{
if(x==y) return true;
for(int i=;i<=id;i++)
if(!tvis[i]&&!vis[i]&&G[x][i])
{
tvis[i]=true;
if(Reach(i,y)) return true;
}
return false;
}
void solve()
{
memset(vis,false,sizeof(vis));
for(int i=;i<=id;i++)
{
memset(tvis,false,sizeof(tvis));
tvis[be]=true;
if(!Reach(i,en)) vis[i]=true; //能否到终点
}
for(int i=;i<=id;i++)
{
memset(tvis,false,sizeof(tvis));
tvis[en]=true;
if(!Reach(i,be)) vis[i]=true; //能否到起点
}
maxl=; //最大长度
dfs(be,); //深搜
}
int main()
{
int T,Case=;
cin>>T;
while(T--)
{
cin>>N;
if(!input()) //无解
{
printf("Case %d: what a pity\n",++Case);
continue;
}
solve();
printf("Case %d:",++Case);
for(int i=;i<=maxl;i++) cout<<" "<<S[ans[i]];
cout<<endl;
}
return ;
}

Hdu3812-Sea Sky(深搜+剪枝)的更多相关文章

  1. poj1190 生日蛋糕(深搜+剪枝)

    题目链接:poj1190 生日蛋糕 解题思路: 深搜,枚举:每一层可能的高度和半径 确定搜索范围:底层蛋糕的最大可能半径和最大可能高度 搜索顺序:从底层往上搭蛋糕,在同一层尝试时,半径和高度都是从大到 ...

  2. UVA 10160 Servicing Stations(深搜 + 剪枝)

    Problem D: Servicing stations A company offers personal computers for sale in N towns (3 <= N < ...

  3. ACM 海贼王之伟大航路(深搜剪枝)

    "我是要成为海贼王的男人!" 路飞他们伟大航路行程的起点是罗格镇,终点是拉夫德鲁(那里藏匿着"唯一的大秘宝"--ONE PIECE).而航程中间,则是各式各样的 ...

  4. hdu 1518 Square(深搜+剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题目大意:根据题目所给的几条边,来判断是否能构成正方形,一个很好的深搜应用,注意剪枝,以防超时! ...

  5. POJ-1724 深搜剪枝

    这道题目如果数据很小的话.我们通过这个dfs就可以完成深搜: void dfs(int s) { if (s==N) { minLen=min(minLen,totalLen); return ; } ...

  6. 一本通例题-生日蛋糕——题解<超强深搜剪枝,从无限到有限>

    题目传送 显然是道深搜题.由于蛋糕上表面在最底层的半径确认后就确认了,所以搜索时的面积着重看侧面积. 找维度/搜索面临状态/对象:当前体积v,当前外表面面积s,各层的半径r[],各层的高度h[]. 可 ...

  7. 模拟赛T5 : domino ——深搜+剪枝+位运算优化

    这道题涉及的知识点有点多... 所以还是比较有意思的. domino 描述 迈克生日那天收到一张 N*N 的表格(1 ≤ N ≤ 2000),每个格子里有一个非 负整数(整数范围 0~1000),迈克 ...

  8. POJ2044 深搜+剪枝(云彩下雨)

    题意:        有一个城镇,是4*4的大小的,然后你控制一块云彩,2*2的,你每天可以有9种走的方法,上下左右,或者不动,走的时候可以走1或者2步,云彩所在的地方肯定会下雨,然后给你做多365天 ...

  9. HDU 1175 连连看 (深搜+剪枝)

    题目链接 Problem Description "连连看"相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以 ...

随机推荐

  1. HashMap Collision Resolution

    Separate Chaining Use data structure (such as linked list) to store multiple items that hash to the ...

  2. centos6.4安装Vmware exsi CLI

    1,Vmware官网Exsi CLI下载链接 https://download2.vmware.com/software/sdk/VMware-vSphere-CLI-4.1.0-254719.x86 ...

  3. Erlang ODBC 处理中文

    erlang处理utf8字符集相对比较简单,因为它是用integer的list来保存所有的string的,所以处理什么字符集都没关系. 话虽这么说,但我在使用erlang的ODBC处理中文时,着实费了 ...

  4. lucene3.6.0 经典案例 入门教程

    第一步:下载并导入lucene的核心包(注意版本问题):  例如Lucene3.6版本:将lucene-core-3.6.0.jar拷贝到项目的libs 文件夹里.  例如Lucene4.6版本:将l ...

  5. select与epoll分析

    关于select与epoll的区别,网上的文章已是一大堆.不过别人的终究是别人的,总得自己去理解才更深刻.于是在阅读了大量的文章后,再装模作样的看下源码,写下了自己的一些理解. 在开始之前,要明白li ...

  6. (转)iOS Wow体验 - 第六章 - 交互模型与创新的产品概念(2)

    本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第六章译文精选的第二部分,其余章节将陆续放出.上一 ...

  7. Windows7 32位机上,OpenCV中配置GPU操作步骤

    1.  查看本机配置,查看显卡类型是否支持NVIDIA GPU: 2.  从http://www.nvidia.cn/Download/index.aspx?lang=cn下载最新驱动并安装: 3.  ...

  8. struts1配置文件之input

    <action path="/*Person" type="cn.itcast.PersonAction" scope="request&quo ...

  9. 常见HTTP状态码的含义

    200 请求已成功,请求所希望的响应头或数据体将随此响应返回. 301 被请求的资源已永久移动到新位置. 302 请求的资源现在临时从不同的 URI 响应请求. 400 1.语义有误,当前请求无法被服 ...

  10. (转)Java 代码优化过程的实例介绍

    简介: 通过笔者经历的一个项目实例,本文介绍了 Java 代码优化的过程,总结了优化 Java 程序的一些最佳实践,分析了进行优化的方法,并解释了性能提升的原因.从多个角度分析导致性能低的原因,并逐个 ...