POJ 1386 Play on Words(欧拉图的判断)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11838 | Accepted: 4048 |
Description
There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word ``acm'' can be followed by the word ``motorola''. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.
Input
Output
If there exists such an ordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.".
Sample Input
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
Sample Output
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
题目链接:POJ 1386
做法:把单词看成有向边,比如acm就是从a->m的一条边,但是字母肯定不能当节点啊,由于字母都是lowercase,那就都减掉'a'来映射到数字0~25就可以了(单词的长度其实在这个题里显然没什么卵用)然后进行欧拉图判断
对于是否是欧拉图的判断:首先基图要连通,然后再看入度和出度不相等的点的个数,若为0,则为欧拉图且有欧拉回路;如果为1,即只有一个点入度不等于其他点均相等,则出度不会等于入度,显然这在图里是不可能的,图的入度和一定会等于出度和;然后是两个点,看是否一个是入度比出度少1,一个是入度比出度多1……,忘记判断vector为空RE几次…………
代码:
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=100010;
const int L=1005;
char s[L];
struct edge
{
int to;
int pre;
};
edge E[N<<1];
int head[N],tot;
int cur[30],vis[30];
int ru[30],chu[30];
void init()
{
CLR(head,-1);
tot=0;
CLR(vis,0);
CLR(cur,0);
CLR(ru,0);
CLR(chu,0);
}
void add(int s,int t)
{
E[tot].to=t;
E[tot].pre=head[s];
head[s]=tot++;
}
bool bfs(int s,int all)
{
queue<int>Q;
vis[s]=1;
--all;
Q.push(s);
while (!Q.empty())
{
int now=Q.front();
Q.pop();
for (int i=head[now]; ~i; i=E[i].pre)
{
int v=E[i].to;
if(!vis[v])
{
vis[v]=1;
--all;
Q.push(v);
}
}
}
return !all;
}
int main(void)
{
int tcase,n,i,a,b,len;
scanf("%d",&tcase);
while (tcase--)
{
init();
scanf("%d",&n);
int P=0;
while (n--)
{
scanf("%s",s);
len=strlen(s);
a=s[0]-'a';
b=s[len-1]-'a';
if(!cur[a])
cur[a]=1,++P;
if(!cur[b])
cur[b]=1,++P;
add(a,b);
add(b,a);
++chu[a];
++ru[b];
}
int s=0;
for (i=0; i<26; ++i)
{
if(cur[i])
{
s=i;
break;
}
}
bool flag=bfs(s,P);
if(!flag)
puts("The door cannot be opened.");
else
{
vector<pii>pos;
for (i=0; i<26; ++i)
{
if((ru[i]==0&&chu[i]==0)||(ru[i]==chu[i]))
continue;
pos.push_back(pii(ru[i],chu[i]));
}
if(pos.empty())
flag=true;
else if(pos.size()>2)
flag=false;
else
{
if(pos[0].first==pos[0].second+1&&pos[1].first==pos[1].second-1)
flag=true;
else if(pos[1].first==pos[1].second+1&&pos[0].first==pos[0].second-1)
flag=true;
else
flag=false;
}
puts(flag?"Ordering is possible.":"The door cannot be opened.");
}
}
return 0;
}
POJ 1386 Play on Words(欧拉图的判断)的更多相关文章
- POJ 1745 线性和差取余判断
POJ 1745 线性和差取余判断 题目大意:每个数都必须取到,相加或相减去,问所有的方案最后的得数中有没有一个方案可以整除k 这个题目的难点在于dp数组的安排上面 其实也就是手动模仿了一下 比如 一 ...
- POJ 1386 Play on Words(单词建图+欧拉通(回)路路判断)
题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...
- POJ 1637 Sightseeing tour (SAP | Dinic 混合欧拉图的判断)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6448 Accepted: 2654 ...
- POJ 1386 判断欧拉回路
题意:要开启一扇门,n个单词是密码,n个单词中,如果一个单词的首字母和前一个单词的尾字母相同,并且每个单词都能这么连起来且只用一次,则门可以开启,否则不能开启,现给出单词,判断门是否可以开. 有向图欧 ...
- poj 1386 Play on Words 有向欧拉回路
题目链接:http://poj.org/problem?id=1386 Some of the secret doors contain a very interesting word puzzle. ...
- poj 1386 Play on Words(有向图欧拉路+并查集)
题目链接:http://poj.org/problem?id=1386 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指 ...
- poj 1386 Play on Words门上的单词【欧拉回路&&并查集】
题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...
- POJ 1386 Play on Words(欧拉路)
http://poj.org/problem?id=1386 题意: 给出多个单词,只有单词首字母与上一个单子的末尾字母相同时可以连接,判断所有字母是否可以全部连接在一起. 思路: 判断是否存在欧拉道 ...
- [POJ 1386] Play on Words
[题目链接] http://poj.org/problem?id=1386 [算法] 将每个单词的首字母向尾字母连一条有向边,判断欧拉路径是否存在,即可 [代码] #include <algor ...
随机推荐
- MFC 颜色选择对话框、颜色按钮
COLORREF color=RGB(0,255,0); unsigned char r=GetRValue(color); unsigned char g=GetGValue(color); uns ...
- Paths on a Grid(poj 1942)
给定一个矩形网格的长m和高n,其中m和n都是unsigned int32类型,一格代表一个单位,就是一步,求从左下角到右上角有多少种走法,每步只能向上或者向右走. //注意循环的时候,要循环小的数,否 ...
- 归并排序的分析与Java实现
归并操作(merge),也叫归并算法,指的是将两个已经排序的序列合并成一个序列的操作.归并排序算法依赖归并操作.该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.归并排序 ...
- Codeigniter:如何写一个好的Model
本文是关于在Code Igniter PHP MVC框架中如何编写Model方法. CRUD 方法 CRUD 是Create, Retrieve, Update, and Delete的缩写. 这些是 ...
- div img居中的方式
想让div中的img水平和垂直都居中,可以将img放在div中,img的样式:height:100%;width:100%; 外部定义div的宽度和高度,然后定义line-height行高,div外部 ...
- blend 从无到有系列之添加自定义Rectangle样式指定到资源文件
相关链接 http://www.cnblogs.com/wildfeng/archive/2012/03/30/2425248.html http://www.cnblogs.com/jv9/arch ...
- laravel框架session使用教程
laravel是一款php框架了,在使用laravel时会碰到session使用问题了,在使用过程中碰到一些问题与一些应用的例子. 用Laravel开发应用,把原有的代码copy过来,以前的代码ses ...
- MATLAB信号与系统分析(一)——连续时间信号与系统的时域分析
一.连续时间信号的表示: 1.向量表示法: 在MATLAB中,是用连续信号在等时间间隔点的样值来近似表示连续信号,当取样时间间隔足够小时,这些离散的样值就能较好地近似出连续信号. 对于连续时间信号f( ...
- Html的一点点收获
结束了牛腩,总结了自己的收获,我开始了征战HTML的计划,在看<提高班培养计划>的时候,我很诧异,因为<HTML孙鑫>这个项目竟然就只有一天的时间,怎么可以这样,但是,我还是决 ...
- vs2010如何生成EXE文件如何更改EXE程序图标
vs2010如何生成EXE文件 F5启动调试后,进入下面路径查找: 我的文档\Visual Studio 2010\Projects\项目名称\项目名称\bin\Debug 如何更改EXE程序图标 其 ...