POJ1386Play on Words(欧拉回路)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11748 | Accepted: 4018 |
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
input consists of T test cases. The number of them (T) is given on the
first line of the input file. Each test case begins with a line
containing a single integer number Nthat indicates the number of plates
(1 <= N <= 100000). Then exactly Nlines follow, each containing a
single word. Each word contains at least two and at most 1000 lowercase
characters, that means only letters 'a' through 'z' will appear in the
word. The same word may appear several times in the list.
Output
program has to determine whether it is possible to arrange all the
plates in a sequence such that the first letter of each word is equal to
the last letter of the previous word. All the plates from the list must
be used, each exactly once. The words mentioned several times must be
used that number of times.
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.
【题意】这个就相当于成语接龙,需满足前一个单词的尾字母与后一个的首字母相同。然后就是问是否 存在欧拉通路。
【分析】用两个数组存每个字母的入度与出度,再用vis[]判断字母是否出现,然后并查集判断是否联通。
定理1:无向图G存在欧拉通路的条件是:G为连通图,并且G只有两个奇度节点或者无奇度节点。
推论1:(1)当G是仅有两个奇度节点的连通图时,G的欧拉通路必以此两个节点为端点。
(2)当G是无奇度节点连通图时,G比为欧拉回路。
(3)G为欧拉图(存在欧拉回路)的充要条件是G为无奇度节点的连通图。
定理2:有向图D存在欧拉通路的充要条件是:D为有向图,D的基图联通,并且所有顶点的出度与入度都相等;或者除两个顶点外
其余顶点的出度与入度都相等,而这两个顶点中一个顶点的出度-入度==1,另一个出度-入度==-1;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
const int M=;
ll power(ll a,int b,ll c){ll ans=;while(b){if(b%==){ans=(ans*a)%c;b--;}b/=;a=a*a%c;}return ans;}
char str[N];
int n,m;
int vis[],pre[];
int out[],in[];
struct man
{
int u,v;
}edg[N];
void init()
{
memset(vis,,sizeof(vis));
memset(out,,sizeof(out));
memset(in,,sizeof(in));
for(int i=;i<;i++)pre[i]=i;
}
int Find(int x) {
if(pre[x] != x) pre[x] = Find(pre[x]);
return pre[x];
} void Union(int x,int y) {
x = Find(x);
y = Find(y);
if(x == y) return;
pre[y] = x;
}
bool beconnect()
{
for(int i=;i<n;i++){
int u=edg[i].u,v=edg[i].v;
if(u!=v&&Find(u)!=Find(v))Union(u,v);
}
int f=-,k;
for(k=;k<;k++){
if(!vis[k])continue;
if(f==-)f=k;
else if(Find(k)!=Find(f))break;
}
if(k<)return false;
return true;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
init();
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s",str);
int u=str[]-'a',v=str[strlen(str)-]-'a';
edg[i].u=u;edg[i].v=v;
vis[u]=vis[v]=;
out[u]++;in[v]++;
}
bool flag=true;
int cnt1=,cnt2=;
for(int i=;i<;i++){
if(!vis[i])continue;
if(abs(out[i]-in[i])>){
flag=false;
break;
}
if(out[i]-in[i]==){
cnt1++;
if(cnt1>){
flag=false;
break;
}
}
if(out[i]-in[i]==-){
cnt2++;
if(cnt2>){
flag=false;
break;
}
}
}
if(cnt1!=cnt2)flag=false;
if(!beconnect())flag=false;
if(flag)puts("Ordering is possible.");
else puts("The door cannot be opened.");
}
return ;
}
POJ1386Play 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 */ ...
随机推荐
- Android 异步通信:图文详解Handler机制工作原理
前言 在Android开发的多线程应用场景中,Handler机制十分常用 今天,我将图文详解 Handler机制 的工作原理,希望你们会喜欢 目录 1. 定义 一套 Android 消息传递机制 2. ...
- BZOJ 3224 Tyvj 1728 普通平衡树 | Splay 板子+SPlay详细讲解
下面给出Splay的实现方法(复杂度证明什么的知道是 nlogn 就可以啦) 首先对于一颗可爱的二叉查找树,是不能保证最坏nlogn的复杂度(可以想象把一个升序序列插入) (二叉查找树保证左子树元素大 ...
- nowcoder 提高组模拟赛 最长路 解题报告
最长路 链接: https://www.nowcoder.com/acm/contest/178/A 来源:牛客网 题目描述 有一张 \(n\) 个点 \(m\) 条边的有向图,每条边上都带有一个字符 ...
- Small things are better
Yesterday I had fun time repairing 1.5Tb ext3 partition, containing many millions of files. Of cours ...
- MySQL备份之mysqlhotcopy与注意事项
此文章主要向大家介绍的是MySQL备份之mysqlhotcopy与其在实际操作中应注意事项的描述,我们大家都知道实现MySQL数据库备份的常用方法有三个,但是我们今天主要向大家介绍的是其中的一个比较好 ...
- [模拟赛] GotoAndPlay
GotoAndPlay 10月3日,在杭州市西湖景区,一只小松鼠不停地接受一道道食物,花生. 玉米.饼干,可谓来者不拒,憨态可掬的模样吸引了众多围观者... Description 小松鼠终于吃撑了, ...
- css的padding,border,margin的意思
下面的内边距(padding).边框(border) .外边距(margin)的意思是我自己的理解. 代码如下: <!DOCTYPE html> <html lang="e ...
- pip 使用国内源
常用国内的pip源如下:阿里云 http://mirrors.aliyun.com/pypi/simple/中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple ...
- jquery中lhgdialog插件(一)
一:前言 最近在使用jquery的控件,其实以前也写但是突然之间遇到了需要从弹出窗口传值到父窗口,突然觉得这种传值的方式其实也是需要javascript的基础的,但是我自己还没有去真正的做过,所以还是 ...
- POST提交数据太大
2018.4.8号更新 其实后来最终的解决方案是修改服务器的配置文件. POST数据按道理说是没有大小限制的,只是取决于浏览器或服务器的配置,tomcat的解决方式参考方案2. ----------- ...