poj 1386 Play on Words
来源:互联网 发布:2017正规淘宝代刷平台 编辑:程序博客网 时间:2024/06/05 05:32
//poj 1386 Play on Words 欧拉回路
/*
注意连通性,为此WA了很多次哎。。。
*/
#include <iostream>
#include <algorithm>
using namespace std;
const int inf = 1<<28;
int n,m;
int in[26],out[26],con[26][26],mark[26];
char input[1010];
void dfs(int v)
{
mark[v]=1;
for (int i=0;i<26;i++)
if (!mark[i] && (con[v][i] || con[i][v]))
dfs(i);
}
bool connect(int v)
{
memset(mark,0,sizeof(mark));
dfs(v);
for (int i=0;i<26;i++)
if ( (in[i] || out[i]) && !mark[i])
return false;
return true;
}
int main()
{
int t,l;scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
memset(con,0,sizeof(con));
for (int i=0;i<n;i++)
{
scanf("%s",input);
l=strlen(input);
out[input[0]-'a']++;
in[input[l-1]-'a']++;
con[input[0]-'a'][input[l-1]-'a']=1;
}
//任意找一个有输入的点进行连通性检测
for (l=0;l<26;l++)
if (in[l] || out[l]) break;
if (!connect(l))
{
printf("The door cannot be opened./n");
continue;
}
//检测出入度,仅允许出度与入度相差一的点各一个
int ind=0,outd=0,i;
for (i=0;i<26;i++)
{
if (in[i]-out[i]==1) ind++;
else if (in[i]-out[i]==-1) outd++;
if (abs(in[i]-out[i])>1 || ind>1 || outd>1) break;
}
if (i>=26) printf("Ordering is possible./n");
else printf("The door cannot be opened./n");
}
system("pause");
return 0;
}
- POJ 1386 Play on Words
- poj 1386 Play on Words
- poj 1386 Play on Words
- poj 1386 Play on Words
- poj 1386 Play on Words
- poj 1386 Play on Words
- POJ 1386 Play on Words
- POJ 1386 Play on Words
- POJ-1386-Play on Words
- POJ 1386 Play on Words
- poj 1386 Play on Words
- poj 1386 Play on Words
- POJ 1386 Play on Words
- poj Play on Words(1386)
- POJ-1386 Play on Words
- poj 1386 Play on Words
- POJ 1386 Play on Words
- POJ 1386 Play on Words
- SQLServer数据库连接(一)
- 使用Mutex来保证单一进程实例的一种方法
- poj1306 Combinations
- C/C++实现split分割字符串
- 4.13 基本数据类型
- poj 1386 Play on Words
- 理解Javascript作用域
- 用mkisofs制作iso文件
- 在vs2010下学《directx9.0 3D 游戏开发编程基础》
- 开通空间了
- 几种常用函数的原型
- 剩余时间倒计时
- vc写入注册表,电脑重启后,写入内容消失原因
- 网页模仿QQ消息