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 ...
随机推荐
- <td>内容超出自动换行
td 内容自动换行 table表格td设置宽度后文字太多自动换行 设置table 的 style="table-layout:fixed;" 然后设置td的 style=" ...
- 《区块链100问》第13集:比特币和Q币有哪些不同?
比特币是一种去中心化的数字资产,没有发行主体.Q币是由腾讯公司发行的电子货币,类似于电子积分,其实不是货币. Q币需要有中心化的发行机构,Q币因为腾讯公司的信用背书,才能被认可和使用.使用范围也局限在 ...
- E. Andrew and Taxi(二分+拓扑判环)
题目链接:http://codeforces.com/contest/1100/problem/E 题目大意:给你n和m,n代表有n个城市,m代表有m条边,然后m行输入三个数,起点,终点,花费.,每一 ...
- Maven部署dao工程到私服上——(十三)
1.修改settings.xml 需要在客户端即(部署dao工程)的电脑上配置 maven环境,并修改 settings.xml 文件,配置连接私服的用户和密码 . 此用户名和密码用于私服校验,因为私 ...
- 转:存储之直连存储Dell Powervault MD 3000
存储之直连存储DellPowervault MD 3000 存储根据服务器类型可以分为:封闭系统的存储和开放系统的存储 1.封闭系统的存储:封闭系统主要指大型机,AS400等服务器 2.开放系统的存储 ...
- Plus One & Plus One Linked List
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- php删除非空目录代码实现
<?php header("Content-type: text/html; charset=utf-8"); $dir='mydir'; function deldir($ ...
- Debian/Ubuntu安装WPS (转)
卸载libreoffice sudo apt-get remove --purge libreoffice* 官网下载WPShttp://wps-community.org/downloads?vl= ...
- Python 3之str类型、string模块学习笔记
Windows 10家庭中文版,Python 3.6.4, Python 3.7官文: Text Sequence Type — str string — Common string operatio ...
- 网络协议之TLS
前言 由于在TCP.UDP等方式传输数据时,数据包有可能被其他人截获,并解析出信息,这就给信息安全带来了很大的挑战.最初的SSL协议被网景公司提出,它不会影响上层协议(如HTTP.电子邮件等),但可以 ...