Play on Words(欧拉回路)
Description
There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word ``acm'' can be followed by the word ``motorola''. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.
Input
Output
If there exists such an ordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.".
Sample Input
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
Sample Output
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int par[];
int id[];
int od[]; ///par为并查集,id为入度,od为出度
int vis[]; ///vis为该顶点出现过的标记
int finds(int x)///并查集的查找函数
{
int r = x;
while(r != par[r])
{
r = par[r];
}
int i = x, j;
while(i != r)///路径压缩
{
j = par[i];
par[i] = r;
i = j;
}
return r;
}
void join(int x, int y)///并查集的合并函数
{
int fx = finds(x);
int fy = finds(y);
if(fx != fy)///如果x,y在两个不同的连通分量上,那么合并
{
par[fy] = fx;
}
}
int main()
{
int t, n, i;
int u,v;
char str[];
scanf("%d", &t);
while(t--)
{
memset(id, , sizeof(id));///初始化各个数组
memset(od, , sizeof(od));
memset(vis, false, sizeof(vis));
for(i = ; i <; i++)///初始化并查集,根节点为自身
{
par[i] = i;
}
scanf("%d", &n);
while(n--)
{
scanf("%s", str);
int len = strlen(str);
u=str[]-'a';
v=str[len-]-'a';
join(u,v);///合并出现的顶点
id[v]++;///相应的入度+1
od[u]++;///相应的出度+1
vis[u]=vis[v]=;///标记该顶点出现过
}
int flag = , tag = , a = , b = ; ///flag来判图是否连通,tag为图中顶点入度不等于出度的个数
///a为入度大出度1的点的个数,b为出度大入度1的点的个数
for(i = ; i <; i++)///判连通
{
if(vis[i] && par[i] == i)///如果连通,那么所有的点将被划分到一个集合之中
{
flag++;
}
}
if(flag > )///不连通,直接输出
{
printf("The door cannot be opened.\n");
}
else///flag必须为1
{
for(i = ; i <; i++)///查找tag,a, b 的个数
{
if(vis[i] && id[i] != od[i])
{
tag++;
}
if(vis[i] && id[i]-od[i] == )
{
a++;
}
if(vis[i] && od[i]-id[i] == )
{
b++;
}
}
if(tag == )///tag = 0,对于所有的点入度等于初度,存在欧拉回路
{
printf("Ordering is possible.\n");
}
else if(a == b && a == && tag == )///a = 1 && b = 1 && tag = 2.存在欧拉通路
{
printf("Ordering is possible.\n");
}
else
{
printf("The door cannot be opened.\n");
}
}
}
return ;
}
Play on Words(欧拉回路)的更多相关文章
- ACM/ICPC 之 混合图的欧拉回路判定-网络流(POJ1637)
//网络流判定混合图欧拉回路 //通过网络流使得各点的出入度相同则possible,否则impossible //残留网络的权值为可改变方向的次数,即n个双向边则有n次 //Time:157Ms Me ...
- [poj2337]求字典序最小欧拉回路
注意:找出一条欧拉回路,与判定这个图能不能一笔联通...是不同的概念 c++奇怪的编译规则...生不如死啊... string怎么用啊...cincout来救? 可以直接.length()我也是长见识 ...
- ACM: FZU 2112 Tickets - 欧拉回路 - 并查集
FZU 2112 Tickets Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u P ...
- UVA 10054 the necklace 欧拉回路
有n个珠子,每颗珠子有左右两边两种颜色,颜色有1~50种,问你能不能把这些珠子按照相接的地方颜色相同串成一个环. 可以认为有50个点,用n条边它们相连,问你能不能找出包含所有边的欧拉回路 首先判断是否 ...
- POJ 1637 混合图的欧拉回路判定
题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定 ...
- codeforces 723E (欧拉回路)
Problem One-Way Reform 题目大意 给一张n个点,m条边的无向图,要求给每条边定一个方向,使得最多的点入度等于出度,要求输出方案. 解题分析 最多点的数量就是入度为偶数的点. 将入 ...
- UVa 12118 检查员的难题(dfs+欧拉回路)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 10054 (欧拉回路) The Necklace
题目:这里 题意:有一种由彩色珠子连接而成的项链,每个珠子两半由不同颜色(由1到50的数字表示颜色)组成,相邻的两个珠子在接触的地方颜色相同,现在有一些零碎的珠子,确认它是否能 复原成完整的项链. 把 ...
- poj2513Colored Sticks(无向图的欧拉回路)
/* 题意:将两端涂有颜色的木棒连在一起,并且连接处的颜色相同! 思路:将每一个单词看成一个节点,建立节点之间的无向图!判断是否是欧拉回路或者是欧拉路 并查集判通 + 奇度节点个数等于2或者0 */ ...
- poj 1386 Play on Words(有向图欧拉回路)
/* 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 再次强调有向图欧拉路或欧拉回路的判 ...
随机推荐
- canvas制作的烟花效果
最近感觉canvas挺有意思的,在业余时间没事研究了一下,参考过网上一些思路,话不多说,开始啦. github地址:https://github.com/aWhiteBear/fireworks 演示 ...
- CentOS6安装各种大数据软件 第六章:HBase分布式集群的配置
相关文章链接 CentOS6安装各种大数据软件 第一章:各个软件版本介绍 CentOS6安装各种大数据软件 第二章:Linux各个软件启动命令 CentOS6安装各种大数据软件 第三章:Linux基础 ...
- Linux --- Ubuntu16.04.5 LTS 虚拟机安装后的软件安装基础操作总结
1. 配置安装源 因为默认是使用Ubuntu官方服务器,国内电脑使用外国服务器较慢,所以需使用国内的服务器(以下清华大学服务器为例). 方法一: (此过程很慢,实在不动就取消吧,加载一部分也够用,以后 ...
- else的妙用--if-else 成对出现,可以检测意外的放生,方便找bug
else用于意外情况的检测.可以写入log,或者串口打印出来. 养成这样的习惯,好处 1 提高程序稳定性,可靠性. 2 遇到bug有头绪.
- js 修改字符串中某些字符的样式
var str = 'abcdefghijklmnobqrstuvwxyz'; function HightLight(e){ var reg = new RegExp(e, 'g') str = s ...
- AC自动机(简单版)(施工ing)
声明 想看加强版的戳这里(施工ing,作者正努力中)~ 先贴题目吧哎~ AC自动机加强版 洛谷 P3796 题目: 洛谷 P3808 (数据范围困了我好久 TAT) 反正涉及字符串的算法都很玄学 ...
- nodejs fastdfs
node端fastdfs客户端上传文件 var FdfsClient = require('fdfs'); var fdfs = new FdfsClient({ // tracker servers ...
- vs2013发布网站合并程序是出错(ILmerge.merge:error)
Vs2013发布网站时,生成错误提示: 合并程序集时出错: ILMerge.Merge: ERROR!!: Duplicate type 'manage_ForcePasswrod' found in ...
- Linux入门进阶第一天——vim文本编辑器
一.VI / VIM概述 [更新]:VIM资料参见:http://www.runoob.com/linux/linux-vim.html 是什么? 是一个文本编辑器. Vim是从 vi 发展出来的一个 ...
- 20155233 2016-2017-2《Java程序设计》课程总结
20155233 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:预习去写博客,如何达到理想的师生关系. 预备作业2:对自己C语言的学习进行了解与认识. ...