[POJ 1386] Play on Words
[题目链接]
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的更多相关文章
- POJ 1386 Play on Words(欧拉图的判断)
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11838 Accepted: 4048 De ...
- 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 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...
- [欧拉回路] poj 1386 Play on Words
题目链接: http://poj.org/problem?id=1386 Play on Words Time Limit: 1000MS Memory Limit: 10000K Total S ...
- 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 ...
- poj 1386 Play on Words(有向图欧拉回路)
/* 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 再次强调有向图欧拉路或欧拉回路的判 ...
随机推荐
- Java基础(一)--操作符
Java底层都是使用操作符来操作Java中的数据 常见的操作符:+.-.*./.= 优先级: 当一个表达式存在多个操作符时,操作符的优先级决定了计算顺序,这点在我们刚开始学习数学的时候就会了解到 如果 ...
- spring cloud (一):大话 Spring Cloud
转自:http://www.ityouknow.com/springcloud/2017/05/01/simple-springcloud.html 研究了一段时间Spring Boot了准备向Spr ...
- SwiftyUserDefaults对NSUserDefaults的封装扩展
SwiftyUserDefaults 是对NSUserDefaults的一些封装和扩展,这个库这个只有一个类,操作起来十分简单方便: 这里只有两个步骤来使用SwiftyUserDefaults: st ...
- swift--字符串替换/过滤/切割
//替换 var ReplaceString = "http://www.aimonkey.cn"; var FilterReplace = ReplaceString.strin ...
- oracle的分号、斜杠和commit
;分号表示一个语句的结束 //表示执行前面的一个代码块,例如begin/end,代码块后面必须跟/才能执行. commitcommit表示提交一个事务,例如insert,delete,update等, ...
- 微信小程序 导航 4种页面跳转 详解
1.wx.navigateTo 保留当前页面,跳转到应用内的某个页面,目前页面路径最多只能十层. 参数:url(可携带参数) .success .fail .complete 可用wxml代替: ...
- Python学习第二阶段day1 内置函数,序列化,软件目录开发规范
内置函数 1.abs() 求绝对值 2.all() 所有元素为真才返回真 all( [1,1,2,3,-1] ) 值为True 3.any() 所有元素为假才返回假 any([0,0,0 ...
- Mysql Group by 分组取最小的实现方法
表结构如下图:
- Git--删除远程仓库文件但不删除本地仓库资源
我们在使用idea开发的过程中经常会出现新建项目的时候直接把xxx.iml文件也添加到了git trace 当然这并不会出现什么问题,问题是当我们把xxx.iml文件push到我们github上之后, ...
- Linux学习总结(19)——Linux中文本编辑器vim特殊使用方法
1. vim比对功能 在linux的环境下 用于观察两个文件的一致性的时候我们一般用diff这个命令来比对,但是这个命令不能你特别详细的比对出 具体的位置或者行对比.这里就用到了vim的对比功能 vi ...