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(有向图欧拉回路)
/* 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 再次强调有向图欧拉路或欧拉回路的判 ...
随机推荐
- C++练习 | 递归判断二叉树是否同构
#include <iostream> using namespace std; struct Tree { int data; Tree *lchild; Tree *rchild; } ...
- jQuery属性操作之.val()函数
目录 .val()实例方法的三种用法 .val()函数源码 调用形式:$('xxx').val(); 调用形式:$('xxx').val(value); 调用形式:$('xxx').val(funct ...
- Call to a member function allowField() on null 错误总结
Call to a member function allowField() on null 在空对象上调用 allowField() 没有该模型对象无法调用,需要创建相应的模型 错误版本: if ...
- Delphi Android下包含第三方DEX
1.将jar转换为dex call dx --dex -verbose --output=.\output\dex\test_classes.dex --positions=lines .\outpu ...
- HCNA(一)网络传输介质
一 .同轴线缆 介绍:同轴线缆是一种早期的网络传输介质,同轴电缆的得名与它的结构相关,由内导体.外导体.绝缘介质和防护套四部分组成.同样支持10Mbps传输速率.现在已经基本被淘汰,不在应用于企业网络 ...
- 20155215 2016-2017-2 《Java程序设计》第3周学习总结
20155215 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 第四章主要讲了类的构建,数组对象,字符串对象的构建与操作等.要学会区分基本类型与类类 ...
- 20155220 2016-2017-2 《Java程序设计》第八周学习总结
20155220 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 第14章 NIO与NIO2 NIO简介 NIO使用频道来衔接数据结点,在处理数据时,NIO可 ...
- 20155327 2016-2017-2《Java程序设计》课程总结
20155327 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:http://www.cnblogs.com/l97----/p/6160983.htm ...
- Oracle下如何用rman备份到特定的sequence
本文为摘抄,目的为方便日后阅读: http://docs.oracle.com/cd/B12037_01/server.101/b10734/rcmbackp.htm To determine the ...
- lamp环境搭建(centos6.9+apache2.4+mysql5.7+php7.1)
lamp环境搭建(centos6.9+apache2.4+mysql5.7+php7.1) 安装前准备:CentOS 6.9 64位 最小化安装 yum install -y make gcc gcc ...