[题目链接]

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. (转)vim编辑器操作命令大全-绝对全

    周六了,熟悉熟悉vim 命令 学习链接: vim命令大全 http://blog.csdn.net/scaleqiao/article/details/45153379 vim命令小技巧 http:/ ...

  2. Day 14B 网络应用开发

    网络应用开发 发送电子邮件 在即时通信软件如此发达的今天,电子邮件仍然是互联网上使用最为广泛的应用之一,公司向应聘者发出录用通知.网站向用户发送一个激活账号的链接.银行向客户推广它们的理财产品等几乎都 ...

  3. 完善本地搭建的jekyll环境(Windows)

    序:上篇文章虽然在本地搭建好了jekyll环境,但是却存在一些问题,如通过jekyll new创建的站点无法正常跑起来.中文编码有问题.这说明之前搭建的环境有不周之处. PS:因之前自己搭建环境时并未 ...

  4. 洛谷——P1379 八数码难题

    P1379 八数码难题 双向BFS 原来双向BFS是这样的:终止状态与起始状态同时入队,进行搜索,只不过状态标记不一样而已,本题状态使用map来存储 #include<iostream> ...

  5. Python-组合数据类型

    集合类型及操作 >集合类型定义 集合是多个元素的无序组合 -集合类型与数学中的集合概念一致 -集合元素之间无序,每个元素唯一,不存在相同元素 -集合元素不可更改,不能是可变数据类型 -集合用大括 ...

  6. lombok 插件安装

    1. 下载地址: https://plugins.jetbrains.com/plugin/6317-lombok-plugin 2. 选择从本地安装.

  7. C#装饰模式

    using System;using System.Collections.Generic;using System.Text; namespace 装饰模式{    class Person    ...

  8. Codeforces Round #260 (Div. 2) D

    D. A Lot of Games time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. RCC 2014 Warmup (Div. 2) 蛋疼解题总结

    A. Elimination time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  10. hdu 2545 并查集 树上战争

    #include<stdio.h> #include<string.h> #define N 110000 struct node {     int father,count ...