POJ1386Play on Words[有向图欧拉路]
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 11846 | Accepted: 4050 |
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
一些知识:
1、无向图存在欧拉回路条件:每个点度都为偶数
2、无向图存在欧拉路条件:只有两个点度为奇数或每个点度都为偶数
3、有向图存在欧拉回路条件:每个点入度都等于出度
4、有向图存在欧拉路条件:只存在这样两个点:一个点入度等于出度+1,另一个点入度=出度-1,其他每个点入度都等于出度,或者每个点入度都等于出度。
判连通,判条件就好了
奇怪的是,dfs就WA,并查集就可以
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
const int N=1e5+,L=1e3+;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int T,n,ind[],outd[],has[];
char s[L];
//int vis[30],g[30][30];
//void dfs(int u){//printf("dfs %d\n",u);
// vis[u]=1;
// for(int v=0;v<=25;v++)
// if(!vis[v]) dfs(v);
//}
int fa[],root=;
inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
int main(){
T=read();
while(T--){
int flag=;
for(int i=;i<=;i++) ind[i]=outd[i]=has[i]=,fa[i]=i;
//memset(g,0,sizeof(g));
n=read();
for(int i=;i<=n;i++){
scanf("%s",s+);
int len=strlen(s+),u=s[]-'a',v=s[len]-'a';
//g[u][v]=g[v][u]=1;
fa[find(u)]=find(v);root=find(v);
outd[u]++;ind[v]++;
has[u]=has[v]=;
}
//for(int i=0;i<=25;i++) if(has[i]) {dfs(i);break;}
//for(int i=0;i<=25;i++) if(has[i]&&!vis[i]) {flag=0;break;}
for(int i=;i<=;i++) if(has[i]&&find(i)!=root){flag=;break;}
if(flag){
int more=,less=;
for(int i=;i<=;i++){
if(abs(outd[i]-ind[i])>=) {flag=;break;}
if(outd[i]==ind[i]+) more++;
if(outd[i]==ind[i]-) less++;
}
if(flag){
if((more==&&less==)||(more==&&less==)) printf("Ordering is possible.\n");
else flag=;
}
}
if(flag==) printf("The door cannot be opened.\n");
}
}
POJ1386Play on Words[有向图欧拉路]的更多相关文章
- Play on Words(有向图欧拉路)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8571 Accepted: 2997 Description Some ...
- poj 1386 Play on Words(有向图欧拉路+并查集)
题目链接:http://poj.org/problem?id=1386 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指 ...
- 欧拉路&&欧拉回路 概念及其练习
欧拉路: 如果给定无孤立结点图G,若存在一条路,经过图中每边一次且仅一次,这条路称为欧拉路: 如果给定无孤立结点图G,若存在一条回路,经过图中每边一次且仅一次,那么该回路称为欧拉回路. 存在欧拉回路的 ...
- HihoCoder1644 : 完美命名的烦恼([Offer收割]编程练习赛37)(有向图的一笔画问题||欧拉路)
描述 程序员常常需要给变量命名.给函数命名.给项目命名.给团队命名…… 好的名字可以大大提高程序员的主观能动性,所以很多程序员在起名时都会陷入纠结和烦恼. 小Hi希望给新的项目起个完美的名字.首先小H ...
- hdu1161 欧拉路
欧拉路径是指能从一个点出发能够“一笔画”完整张图的路径:(每条边只经过一次而不是点) 在无向图中:如果每个点的度都为偶数 那么这个图是欧拉回路:如果最多有2个奇数点,那么出发点和到达点必定为该2点,那 ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- hihoCoder #1182 欧拉路·三 (变形)
题意: 写出一个环,环上有2^n个格子,每个格子中的数字是0或1,相连着的n个格子可以组成一个数的二进制,要求给出这2^n个数字的序列,使得组成的2^n个数字全是不同的.(即从0到2^n-1) 思路: ...
- hiho 1182 : 欧拉路·三
1182 : 欧拉路·三 这时题目中给的提示: 小Ho:是这样的,每次转动一个区域不是相当于原来数字去掉最左边一位,并在最后加上1或者0么. 于是我考虑对于"XYYY",它转动之后 ...
- Play on Words(欧拉路)
http://poj.org/problem?id=1386 题意:给定若干个单词,若前一个的尾字母和后一个单词的首字母相同,则这两个单词可以连接,问是否所有的单词都能连接起来. 思路:欧拉路的判断, ...
随机推荐
- vue单页面程序
gitHub地址:https://github.com/lily1010/vue_singlePage 举个栗子: <!DOCTYPE html> <html> <hea ...
- SharePoint2010升级到SharePoint2013操作手册
SharePoint2010升级到SharePoint2013操作手册 目 录 第一章 前言 3 第二章 升级前准备 3 第三章 升级流程图 5 第四章 升级过程 5 4.1 ...
- AJAX跨域访问(从Tomcat8到Apache/Nginx)
1.在Tomcat的Root目录下放入如下的文件 apache-tomcat-8.0.12X64\webapps\ROOT clientaccesspolicy.xml文件 <?xml vers ...
- JS常用的function集合
1.把字符串转为日期格式 (1) var str ='2012-08-12 23:13:15';str = str.replace(/-/g,"/");var date = ne ...
- 【Leafletjs】2.添加marker到地图
本人建了一个Leaflet交流群:Leaflet&WebGIS 331437754 接着上篇我们在地图中添加一个marker,非常简单只需添加如下代码即可: var marker = L.m ...
- XMPP学习——1、介绍
XMPP(Extensible Messaging and Presence Protocol,前称Jabber[1])是一种以XML为基础的开放式实时通信协议,是经由互联网工程工作小组(IETF)通 ...
- iOS中响应者链条-触摸事件
总体来说,分2个步骤: 一,从上到下寻找合适的控件来处理这个触摸事件.如下图,如果点击了黄色4,则UIApplication -> UIWindow -> 1白色 -> 2橙色 -& ...
- 自定义button
改变button内部label和imageView的frame - (CGRect)titleRectForContentRect:(CGRect)contentRect - (CGRect)imag ...
- Java 线程异常处理器
Thread.UncaughtExceptionHandler 是Thread类的一个静态内部接口,该接口只有一个方法: void uncaughtException(Thread t, Throwa ...
- mac 远程连接服务器
很多刚用mac的同学 可能会纠结,连接远程服务器咋整? 有没有类型windows上的securecrt 其实,完全可以不用: mac自带的终端就可以搞定:终端terminal 如何连接远程服务器? s ...