[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(有向图欧拉回路)
/* 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 再次强调有向图欧拉路或欧拉回路的判 ...
随机推荐
- 微服务网关从零搭建——(八)Ocelot网关中加入skywalking APM
准备工作 一.下载skywalking 本例使用的是 注: 1.解压后执行完2,3步骤后运行\bin\startup.bat 2.默认后台端口为8080 如需修改则修改\webapp\webapp.y ...
- libevent reference Mannual II--library
FYI: http://www.wangafu.net/~nickm/libevent-book/TOC.html The Libevent Reference Manual: Preliminari ...
- 洛谷——P3919 【模板】可持久化数组(可持久化线段树/平衡树)
P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目背景 UPDATE : 最后一个点时间空间已经放大 标题即题意 有了可持久化数组,便可以实现很多衍生的可持久化功能(例如:可持久化并查集 ...
- 日本語 IME输入法(Microsoft 输入法)切换问题
平假名 与 片假名之间的切换 按住 Ctrl + Caps Lock(平假名) 按住 Alt + Caps Lock(片假名) 另外:语言之间的切换 Alt + Shift 键 / Windows + ...
- Linux 安装 MySQL 详解(rpm 包)
说明:Linux 系统中软件的安装在 root 用户下进行,此安装方式为 rpm 包方式,安装的版本为:MySQL-5.6.25-1.linux_glibc2.5.x86_64.rpm-bundle. ...
- textbook references
* math 1. Teubner-Taschenbuch der Mathematik * CFD
- list数组排序---stream
import java.util.*;import java.util.stream.Collector;import java.util.stream.Collectors; public clas ...
- [bzoj4567][Scoi2016][背单词] (贪心+trie树)
Description Lweb 面对如山的英语单词,陷入了深深的沉思,“我怎么样才能快点学完,然后去玩三国杀呢?”.这时候睿智 的凤老师从远处飘来,他送给了 Lweb 一本计划册和一大缸泡椒,他的计 ...
- [bzoj4247][挂饰] (动规+排序)
Description JOI君有N个装在手机上的挂饰,编号为1...N. JOI君可以将其中的一些装在手机上. JOI君的挂饰有一些与众不同——其中的一些挂饰附有可以挂其他挂件的挂钩.每个挂件要么直 ...
- java多线程synchronized volatile解析
先简单说说原子性:具有原子性的操作被称为原子操作.原子操作在操作完毕之前不会线程调度器中断.即一个操作或者多个操作 要么全部执行并且执行的过程不会被任何因素打断,要么就都不执行.在Java中,对除了l ...