Play on Words
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11748   Accepted: 4018

Description

Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us.

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

The
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

Your
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(欧拉回路)的更多相关文章

  1. ACM/ICPC 之 混合图的欧拉回路判定-网络流(POJ1637)

    //网络流判定混合图欧拉回路 //通过网络流使得各点的出入度相同则possible,否则impossible //残留网络的权值为可改变方向的次数,即n个双向边则有n次 //Time:157Ms Me ...

  2. [poj2337]求字典序最小欧拉回路

    注意:找出一条欧拉回路,与判定这个图能不能一笔联通...是不同的概念 c++奇怪的编译规则...生不如死啊... string怎么用啊...cincout来救? 可以直接.length()我也是长见识 ...

  3. ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

     FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u P ...

  4. UVA 10054 the necklace 欧拉回路

    有n个珠子,每颗珠子有左右两边两种颜色,颜色有1~50种,问你能不能把这些珠子按照相接的地方颜色相同串成一个环. 可以认为有50个点,用n条边它们相连,问你能不能找出包含所有边的欧拉回路 首先判断是否 ...

  5. POJ 1637 混合图的欧拉回路判定

    题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定 ...

  6. codeforces 723E (欧拉回路)

    Problem One-Way Reform 题目大意 给一张n个点,m条边的无向图,要求给每条边定一个方向,使得最多的点入度等于出度,要求输出方案. 解题分析 最多点的数量就是入度为偶数的点. 将入 ...

  7. UVa 12118 检查员的难题(dfs+欧拉回路)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. UVA 10054 (欧拉回路) The Necklace

    题目:这里 题意:有一种由彩色珠子连接而成的项链,每个珠子两半由不同颜色(由1到50的数字表示颜色)组成,相邻的两个珠子在接触的地方颜色相同,现在有一些零碎的珠子,确认它是否能 复原成完整的项链. 把 ...

  9. poj2513Colored Sticks(无向图的欧拉回路)

    /* 题意:将两端涂有颜色的木棒连在一起,并且连接处的颜色相同! 思路:将每一个单词看成一个节点,建立节点之间的无向图!判断是否是欧拉回路或者是欧拉路 并查集判通 + 奇度节点个数等于2或者0 */ ...

随机推荐

  1. [Leetcode] Reorder list 重排链表

    Given a singly linked list L: L 0→L 1→…→L n-1→L n,reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→… You ...

  2. jsp电子商务 购物车实现之二 登录和分页篇

    登录页面核心代码 <div id="login"> <h2>用户登陆</h2> <form method="post" ...

  3. 【BZOJ】2453: 维护队列【BZOJ】2120: 数颜色 二分+分块(暴力能A)

    先说正解:把所有相同的数相成一个链在每一个区间里的种数就是不同链的链头,那么记录每个数的上个相同数所在位置,那么只要找出l到r之间前驱值在l之前的数的个数就可以了 本人打的暴力,有一个小技巧,用cha ...

  4. Educational Codeforces Round 55:A. Vasya and Book

    A. Vasya and Book 题目链接:https://codeforc.es/contest/1082/problem/A 题意: 给出n,x,y,d,x是起点,y是终点,d是可以跳的格数,注 ...

  5. ibeacon UUID

    import sys; import uuid; s=uuid.uuid4().hex #s="f6bc15e0939046679be1866ec8a199dc" sys.stdo ...

  6. Boke光纤交换机的snmp配置

    今天我配置了一下Boke光纤交换机e-300的snmp trap的配置 敲击help命令你会发现配置snmp的命令共有四个 snmpconfig                      Config ...

  7. ActiveMQ(3) ActiveMQ创建(simpleAuthenticationPlugin)安全认证

    控制端安全认证: ActiveMQ目录conf下jetty.xml: <bean id="securityLoginService" class="org.ecli ...

  8. SpringMVC学习 -- 使用 @RequestMapping 映射请求

    在控制器的类定义及方法出定义出都可以标注 @RequestMapping: 类定义处:提供初步的请求映射信息.相对于 Web 应用的根目录. 方法定义出:提供进一步的细分映射信息.相对于类定义处的 U ...

  9. Spring学习-- IOC 容器中 bean 的生命周期

    Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...

  10. 关于javascript中的this 一段小实例深有体会啊

    先声明鄙人正在努力的把脚抬进门来,说的都是比较粗浅的知识,但都是我实践中得出的体会,很深刻.  正在自学中挣扎的DOG. 先看段代码: function highlightRows() { if(!d ...