[题目链接]

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. Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

    WebsocketSourceConfiguration { @Bean ServletWebServerFactory servletWebServerFactory(){ return new T ...

  2. java基础学习之内存分析(栈、堆、方法区)

    栈存放:会为每个方法(包括构造函数)开辟一个栈指针,方法执行完毕后,会自动退出,并释放空间,主要每个方法中的存放局部变量 局部变量   先进后出 自下而上存储 方法执行完毕 自动释放空间 堆: 存放n ...

  3. JS中的let和var的区别

    最近很多前端的朋友去面试被问到let和var的区别,其实阮一峰老师的ES6中已经很详细介绍了let的用法和var的区别.我简单总结一下,以便各位以后面试中使用. ES6 新增了let命令,用来声明局部 ...

  4. ubuntu 16.04 添加网卡

    root@ubuntu:~# ls /sys/class/net/ enp0s3 enp0s8 lo root@ubuntu:~# vim /etc/network/interfaces # This ...

  5. scp 上传文件自动录入密码

    --- 服务器IP地址 des_host=serverIp 服务器存储路径(文件上传后存储指定目录下) des_direc=/home/lk/ 服务器用户密码 des_pass=root_passwo ...

  6. vue子组件向父组件传递参数的基本方式

    子组件: this.$emit('transferUrl', this.picUrl) 父组件: 引入子组件<pics @transferUrl="gettransferUrl&quo ...

  7. vue axios请求超时,设置重新请求的完美解决方法

    //在main.js设置全局的请求次数,请求的间隙 axios.defaults.retry = 4; axios.defaults.retryDelay = 1000; axios.intercep ...

  8. Uva1103 Ancient Messages

    题意:识别图中的象形文字.但是,图形可以任意的拉伸,但不能拉断. 分析:这种题如果图形没有特征是不可做类型的题,不过观察图形可以发现每个图形中的洞的数量是一定的,我们只需要数出每一个封闭图形的洞数就能 ...

  9. 线程间的通信----wait/notify机制

    wait/notify机制 实现多个线程之间的通信可以使用wait.notify.notifyAll三个方法.这三个方法都是Object类的方法.wait():导致当前线程等待,直到另一个线程调用此对 ...

  10. [bzoj3781]小B的询问_莫队

    小B的询问 bzoj-3781 题目大意:给定一个n个数的序列,m次询问.每次询问一段区间内数的种类的平方和. 注释:$1\le n\,m\le 5\cdot 10^4$. 想法:莫队练习题. 我们考 ...