Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u

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.

Source

欧拉道路问题。这题欧拉回路或者欧拉道路都算成立。

建好图以后先判一下连通性,如果图不连通,肯定不成立。

↑DFS BFS 并查集都可以

然后是判欧拉路径。

  如果是欧拉回路,那么所有点的入度等于出度。

  如果是欧拉路径,那么只有两个点的入度不等于出度,其一是起始点,出度比入度大1,另一个是结束点,入度比出度大1

之前一直WA,不知怎么改着改着就对了。

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int mxn=;
bool vis[mxn];
int n;
int fa[mxn];
int in[mxn],out[mxn];
void init(int n){
for(int i=;i<=n;i++)fa[i]=i;
}
int find(int x){
if(fa[x]==x)return x;
else return fa[x]=find(fa[x]);
}
char s[];
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(vis,,sizeof vis);
memset(in,,sizeof in);
memset(out,,sizeof out);
scanf("%d",&n);
init();
int i,j;
for(i=;i<=n;i++){
scanf("%s",s);
int u=s[]-'a'+;
int v=s[strlen(s)-]-'a'+;
in[v]++;out[u]++;
vis[u]=vis[v]=;
int x=find(u),y=find(v);
if(x!=y)fa[y]=x;
}
int st=,ed=;bool flag=;
int cnt=;
for(i=;i<=;i++){
if(vis[i] && find(i)==i){
cnt++;
}
if(out[i]!=in[i]){//出度不等于入度时判定
if(out[i]==in[i]+){
if(!st)st=i;
else flag=;
}
else if(in[i]==out[i]+){
if(!ed)ed=i;
else flag=;
}
else flag=;
}
}
if(cnt!=){//非连通特判
printf("The door cannot be opened.\n");
continue;
}
if(flag==)printf("The door cannot be opened.\n");
else printf("Ordering is possible.\n");
}
return ;
}

POJ1386 Play on Words的更多相关文章

  1. 【poj1386】 Play on Words

    http://poj.org/problem?id=1386 (题目链接) 题意 给出n个单词,判断它们能否首尾相接的排列在一起. Solution 将每一格单词的首字母向它的尾字母连一条有向边,那么 ...

  2. poj-1386(欧拉回路)

    题意:给你n个单词,每个单词可以和另一个单词连接,前提是(这个单词的尾字母等下一个单词的首字母),问你有没有一种连法能够连接所有的单词: 解题思路:每个单词可以看成是首字母指向尾字母的一条边,那么就变 ...

  3. poj1386 字符串类型的一笔画问题 欧拉回路

    Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10685   Accepted: 3625 De ...

  4. poj1386单词连接(欧拉欧拉欧拉)

    ///单词连接,欧拉回路通路都可以(有向图) ///主要构图:比如possibilities就构造p->s的边////题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词 ...

  5. poj1386有向图判断是否存在欧拉回路或者欧拉路

      有向图的图联通是指基图联通,也就是把有向图的边改成无向图然后看是否连通.判断联通可用dfs或者并查集. 题意就是给你n个由小写字母构成的字符串,问你能不能将这n个字符串连接起来,B能接在A后面的条 ...

  6. Play on Words

    poj1386:http://poj.org/problem?id=1386 题意:给你n个单词,问你是否能够通过调整单词的顺序存在这样的一个序列,使得 每个单词的首字母是前一个单词的尾字母. 题解: ...

  7. D2欧拉路,拓扑排序,和差分约束

    第一题:太鼓达人:BZOJ3033 题意:给出k,求一个最长的M位01串,使其从每一个位置向后走k个得到 的M个k位01串互不相同(最后一个和第一个相邻,即是一个环).输出 字典序最小的答案. 2 ≤ ...

  8. ACM学习大纲

    1 推荐题库 •http://ace.delos.com/usaco/ 美国的OI 题库,如果是刚入门的新手,可以尝试先把它刷通,能够学到几乎全部的基础算法极其优化,全部的题解及标程还有题目翻译可以b ...

  9. POJ 1386 Play on Words (有向图欧拉路径判定)

    Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8768   Accepted: 3065 Des ...

随机推荐

  1. 理解volatile与synchronized

    用 volatile 修饰的变量可以保证线程的"可见性",也就是,任何线程修改了这个 volatile 修饰的值都会通知其他线程来主缓存中重新读取值. 下面通过例子加以说明: pu ...

  2. HTML+CSS : 笔记整理(1)

    meta:页面描述信息(可以在里面加入作者信息等,如: <meta name="description"content="HTML examples"&g ...

  3. web worker,SSE,WebSocket,AJAX 与后端交互的方式

    一 web worker web worker 是运行在后台的 JavaScript,独立于其他脚本,不会影响页面的性能.您可以继续做任何愿意做的事情:点击.选取内容等等,而此时 web worker ...

  4. 多线程之ReadWriteLock模拟缓存(九)

    错误案例1: package com.net.thread.lock; import java.util.HashMap; import java.util.Map; import java.util ...

  5. P1414 又是毕业季II (数学?

    题目背景 “叮铃铃铃”,随着高考最后一科结考铃声的敲响,三年青春时光顿时凝固于此刻.毕业的欣喜怎敌那离别的不舍,憧憬着未来仍毋忘逝去的歌.1000多个日夜的欢笑和泪水,全凝聚在毕业晚会上,相信,这一定 ...

  6. Android 意图通用类 IntentUrl

    1.整体分析 1.1.源代码如下,可以直接Copy. public class IntentUtil { /** * 打开链接 * 根据设置判断是用那种方式打开 * * @param context ...

  7. 【转帖】LoadRunner系统架构简介

    LoadRunner系统架构简介: LoadRunner是通过创建虚拟用户来代替真实实际用户来操作客户端软件比如Internet Explorer,来向IIS.Apache等Web服务器发送HTTP协 ...

  8. [bzoj2733]永无乡&&[bzoj3545]Peaks

    并不敢说完全会了线段树合并,只是至少知道原理写法了...还是太菜了,每天被大佬吊锤qwq 我看到的几道线段树合并都是权值线段树的合并.这个算法适用范围应该只是01线段树的. 这两道算入门题了吧... ...

  9. Struts2---数据封装机制

    Struts2属性驱动和模型驱动 自动完成了数据的获取和封装 LoginAction.java public class LoginAction implements ModelDriven<U ...

  10. 3 Mongodb数据查询1

    1.基本查询 方法find():查询 db.集合名称.find({条件文档}) 方法findOne():查询,只返回第一个 db.集合名称.findOne({条件文档}) 方法pretty():将结果 ...