[题目链接]

http://poj.org/problem?id=1386

[算法]

将每个单词的首字母向尾字母连一条有向边,判断欧拉路径是否存在,即可

[代码]

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 100010
#define MAXLEN 1010
#define MAXC 30
const int M = ; struct edge
{
int to,nxt;
} e[MAXN]; int tot;
int fa[MAXC],head[MAXC],size[MAXC],in[MAXC],out[MAXC];
char str[MAXLEN];
set< int > s; template <typename T> inline void read(T &x)
{
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u,int v)
{
tot++;
e[tot] = (edge){v,head[u]};
head[u] = tot;
}
inline int get_root(int x)
{
if (fa[x] == x) return x;
return fa[x] = get_root(fa[x]);
}
inline void merge(int u,int v)
{
int x = get_root(u) , y = get_root(v);
if (x == y) return;
if (size[x] < size[y]) swap(x,y); // Union By Rank
size[x] += size[y];
fa[y] = x;
} int main()
{ int T;
read(T);
while (T--)
{
int n;
read(n);
for (int i = ; i <= ; i++)
{
head[i] = ;
fa[i] = i;
size[i] = ;
in[i] = out[i] = ;
}
s.clear();
for (int i = ; i <= n; i++)
{
scanf("%s",str + );
int len = strlen(str + );
int fir = str[] - 'a' + , lst = str[len] - 'a' + ;
merge(fir,lst);
addedge(fir,lst);
in[lst]++; out[fir]++;
s.insert(fir); s.insert(lst);
}
bool connect = false;
int sz = (int)s.size();
for (int i = ; i <= ; i++) connect |= (size[i] == sz);
if (!connect)
{
printf("The door cannot be opened.\n");
continue;
}
bool flag = true;
for (set<int> :: iterator it = s.begin(); it != s.end(); it++) flag &= (in[*it] == out[*it]);
if (flag)
{
printf("Ordering is possible.\n");
continue;
}
int s1 = , s2 = ;
for (set<int> :: iterator it = s.begin(); it != s.end(); it++)
{
s1 += ((in[*it] - out[*it]) == );
s2 += ((out[*it] - in[*it]) == );
if (abs(in[*it] - out[*it]) >= ) s1 = M;
}
if (s1 == s2 == ) printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n");
} return ; }

[POJ 1386] Play on Words的更多相关文章

  1. POJ 1386 Play on Words(欧拉图的判断)

    Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11838   Accepted: 4048 De ...

  2. poj 1386 Play on Words 有向欧拉回路

    题目链接:http://poj.org/problem?id=1386 Some of the secret doors contain a very interesting word puzzle. ...

  3. poj 1386 Play on Words(有向图欧拉路+并查集)

    题目链接:http://poj.org/problem?id=1386 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指 ...

  4. poj 1386 Play on Words门上的单词【欧拉回路&&并查集】

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

  5. POJ 1386 Play on Words(欧拉路)

    http://poj.org/problem?id=1386 题意: 给出多个单词,只有单词首字母与上一个单子的末尾字母相同时可以连接,判断所有字母是否可以全部连接在一起. 思路: 判断是否存在欧拉道 ...

  6. POJ 1386 Play on Words(单词建图+欧拉通(回)路路判断)

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

  7. [欧拉回路] poj 1386 Play on Words

    题目链接: http://poj.org/problem?id=1386 Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  8. POJ 1386&&HDU 1116 Play on Words(我以后再也不用cin啦!!!)

    Play on Words Some of the secret doors contain a very interesting word puzzle. The team of archaeolo ...

  9. poj 1386 Play on Words(有向图欧拉回路)

    /* 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 再次强调有向图欧拉路或欧拉回路的判 ...

随机推荐

  1. 22万个木箱!TWaver 3D极限压榨

    打开个门户网站都千呼万唤,我们还能期待网页上的3D技术会有“酣畅淋漓”.“一气呵成”的感受吗?也许现在还差点火候.但是HTML5.WebGL等技术一直在飞速的发展,可能很快你就会惊讶它的能力.现在,我 ...

  2. C++ 实现Golang里的defer

    不多说了,直接贴代码.就一个hpp文件. 1 #include <functional> 2 3 #define CONCAT_(a, b) a##b 4 #define CONCAT(a ...

  3. [Algorithm] 9. Two Sum

    Description Given an array of integers, return indices of the two numbers such that they add up to a ...

  4. Swoft 新手向教程 - 通过 Docker 搭建一个开发环境

    本系列文章将从使用层面介绍 Swoft 框架的使用及业务开发,面向初中级的 PHPer Swoft首个基于 Swoole 原生协程的新时代 PHP 高性能协程全栈组件化框架,内置协程网络服务器及常用的 ...

  5. 什么是restful

  6. Python基础(六) 基础文件操作

    今天学习python下对文件的基础操作,主要从open函数.File对象的属性.文件定位.简单操作.举例说明几个步骤开始学习,下面开始进入今天的主题: 一.open函数介绍 open函数主要是打开一个 ...

  7. js cookies all in one

    js cookies all in one cookies // http://10.1.5.202/auto-deploy-platform/publish/index.html // 非当前 UR ...

  8. Shortest Prefixes(poj 2001)

    题意:给出n个单词(1<=n<=1000),求出每个单词的非公共前缀,如果没有,则输出自己. /* 字典树 在裸字典树的基础上,设置一个sum数组,sum[i]表示i这个节点被用过几次,当 ...

  9. 关于使用CELERY的一点心得

    使用也有大半年了.稳定性没话说啊. 但有一个坑,是我以前没注意的,记录下来. 就是本来一个任务是可以异步并行执行的..但如何需要CELERY的执行结果来作判断的话,就会变得异步串行的. 这要值得注意. ...

  10. [bzoj1232][Usaco2008Nov]安慰奶牛cheer_Kruskal

    安慰奶牛 cheer bzoj-1232 Usaco-2008 Nov 题目大意:给定一个n个点,m条边的无向图,点有点权,边有边权.FJ从一个点出发,每经过一个点就加上该点点权,每经历一条边就加上该 ...