POJ 1386 Play on Words (有向图欧拉路径判定)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 8768 | Accepted: 3065 |
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.
Source
用并查集判断连通,然后判断欧拉路径存在。
/* ***********************************************
Author :kuangbin
Created Time :2014-2-3 14:18:41
File Name :E:\2014ACM\专题学习\图论\欧拉路\有向图\POJ1386.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; int F[];
int find(int x)
{
if(F[x] == -)return x;
else return F[x] = find(F[x]);
}
void bing(int u,int v)
{
int t1 = find(u);
int t2 = find(v);
if(t1 != t2)F[t1] = t2;
}
char str[];
int in[],out[];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(F,-,sizeof(F));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
int s = -;
while(n--)
{
scanf("%s",str);
int len = strlen(str);
int u = str[] - 'a';
int v = str[len-] - 'a';
bing(u,v);
out[u]++;in[v]++;
if(s == -)s = u;
}
bool flag = true;
int cc1 = ,cc2 = ;
for(int i = ;i < ;i++)
{
if(out[i] - in[i] == )cc1++;
else if(out[i] - in[i] == -) cc2++;
else if(out[i] != in[i])
flag = false;
if(out[i] || in[i])
if(find(i) != find(s))
flag = false;
}
if( !( (cc1 == && cc2 == ) || (cc1 == && cc2 == ) ) )flag = false;
if(flag)printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n");
}
return ;
}
POJ 1386 Play on Words (有向图欧拉路径判定)的更多相关文章
- poj 1386 Play on Words(有向图欧拉回路)
/* 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 再次强调有向图欧拉路或欧拉回路的判 ...
- POJ 1386 欧拉路的判定
题意: 给你n个单词,问你有没有一种排列方式可以所有单词的首部是相邻单词的尾部. 思路: 这个题目还挺基础的,就是个欧拉的判定,首先对于每一个单词,我们把他抽象成边,每个单词两 ...
- 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 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指 ...
- POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8756 Accepted: 2306 Descript ...
- POJ 1386 有向图欧拉通路
题意:给你一些字符串,这些字符串可以首位相接(末位置如果和另一个字符串的首位置相同的话就可以相连) .然后问你是否可以全部连起来. 思路:就是取出每个字符串的首尾位置,然后求出出度和入度,根据有向欧拉 ...
- 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 [算法] 将每个单词的首字母向尾字母连一条有向边,判断欧拉路径是否存在,即可 [代码] #include <algor ...
随机推荐
- Javascript加速运动与减速运动
加速运动,即一个物体运动时速度越来越快:减速运动,即一个物体运动时速度越来越慢.现在用Javascript来模拟这两个效果,原理就是用setInterval或setTimeout动态改变一个元素与另外 ...
- Jquery 较好的效果
仿google图片效果图片展示相册(jquery)的演示页面 产品相册展示插件slideshow多图可翻页 懒人建站 Jquery分享A Jquery分享B Jquery分享C Jquery分享D
- Linux - Port 端口检测方式
ss 和 netstat 区别 netstat是遍历/proc下面每个PID目录: ss直接读/proc/net下面的统计信息. 所以ss执行的时候消耗资源以及消耗的时间都比netstat少很多 ne ...
- 第11月第14天 opengl yuv beginners-tutorials
1. Here is some snippets of code from my project 'movie player for iOS'. 1. fragment shader varying ...
- centos7系统下安装配置jdk、tomcat教程
JDK安装与配置 1.下载linux版本的jdk,我下的版本是jdk6.0,下载rpm版本的. 可通过百度搜索文件名:jdk-6u45-linux-x64-rpm.bin下载 也可通过oracle官网 ...
- 安装Python3.6.4后,在使用numpy时报错RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
原因: 因为安装numpy用的是 pip来安装的 pypi官方对于numpy的库已经升级了,但是升级后的版本与其他的库不匹配 所以报错 解决: 先把已经安装的numpy卸载: pip uninstal ...
- TimedSupervisorTask
啊啊啊 UnsupportedOperationException Lists.emptyLIst() . add (String[] ) 这他妈的不行.. .2017/09/13 16:42:16. ...
- RandomForest随机森林总结
1.随机森林原理介绍 随机森林,指的是利用多棵树对样本进行训练并预测的一种分类器.该分类器最早由Leo Breiman和Adele Cutler提出,并被注册成了商标.简单来说,随机森林就是由多棵CA ...
- protected
protected 继承访问权限 在同一包中可以访问protected成员. 继承状态可以访问protected成员 在不同包中非继承不可以访问protected成员.
- scanf 输入加逗号(或者不加逗号)出现的异常及解决方案
我们在写 C 语言代码通常 scanf 的格式控制部分都有两种习惯,加逗号与不加逗号,而这两种情况都会因为我们的不同输入习惯产生一定的问题,这里给出另一种方法. 1.不加逗号 #include< ...