poj-1386(欧拉回路)
题意:给你n个单词,每个单词可以和另一个单词连接,前提是(这个单词的尾字母等下一个单词的首字母),问你有没有一种连法能够连接所有的单词;
解题思路:每个单词可以看成是首字母指向尾字母的一条边,那么就变成求欧拉通路的问题了;
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int f[];
int findf(int u)
{
if(f[u]==u)
return u;
else
{
f[u]=findf(f[u]);
return f[u];
}
}
void join(int x,int y)
{
int t1=findf(x);
int t2=findf(y);
f[t2]=t1;
}
int main()
{
int tt;
int n;
int flag;
int cnt1,cnt2;
int father;
int indeg[];
int outdeg[];
char t[];
scanf("%d",&tt);
while(tt--)
{
memset(outdeg,,sizeof(outdeg));
memset(indeg,,sizeof(indeg));
for(int i=;i<;i++)
f[i]=i;
flag=cnt1=cnt2=;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%s",t);
int len=strlen(t);
int x=t[]-'a'+;
int y=t[len-]-'a'+;
outdeg[x]++;
indeg[y]++;
join(x,y);
father=y;
}
for(int i=;i<=;i++)
{
if(outdeg[i]!=||indeg[i]!=)
{
if(findf(i)!=findf(father))
flag=;
}
}
if(flag)
{
printf("The door cannot be opened.\n");continue;
}
for(int i=;i<=;i++)
{
if(indeg[i]!=outdeg[i])
{
if(indeg[i]-outdeg[i]==)
cnt1++;
else if(indeg[i]-outdeg[i]==-)
cnt2++;
else
{
flag=;break;
}
}
}
if(cnt1!=cnt2)
{
flag=;
}
else
{
if(cnt1>)
flag=;
}
if(flag)
printf("The door cannot be opened.\n");
else
printf("Ordering is possible.\n");
}
}
poj-1386(欧拉回路)的更多相关文章
- 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 Some of the secret doors contain a very interesting word puzzle. ...
- poj 1386 Play on Words门上的单词【欧拉回路&&并查集】
题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...
- [欧拉回路] poj 1386 Play on Words
题目链接: http://poj.org/problem?id=1386 Play on Words Time Limit: 1000MS Memory Limit: 10000K Total S ...
- poj 1386 Play on Words(有向图欧拉回路)
/* 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 再次强调有向图欧拉路或欧拉回路的判 ...
- HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)
题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有 ...
- POJ 1386 判断欧拉回路
题意:要开启一扇门,n个单词是密码,n个单词中,如果一个单词的首字母和前一个单词的尾字母相同,并且每个单词都能这么连起来且只用一次,则门可以开启,否则不能开启,现给出单词,判断门是否可以开. 有向图欧 ...
- POJ 1386 Play on Words(单词建图+欧拉通(回)路路判断)
题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...
- poj 1386 Play on Words(有向图欧拉路+并查集)
题目链接:http://poj.org/problem?id=1386 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指 ...
- POJ 1386 Play on Words(欧拉路)
http://poj.org/problem?id=1386 题意: 给出多个单词,只有单词首字母与上一个单子的末尾字母相同时可以连接,判断所有字母是否可以全部连接在一起. 思路: 判断是否存在欧拉道 ...
随机推荐
- Java验证工具类
在项目中使用Java经常有验证功能的使用,比如手机号,密码等验证. 总结一下,写出个工具类方便以后用的时候直接引. package com.common.utils; import org.apach ...
- Scala学习(八)练习
Scala中继承&练习 1. 扩展如下的BankAccount类,新类CheckingAccount对每次存款和取款都收取1美元的手续费 class BankAccount ( initial ...
- 实现可调整宽高的DIV(左右拖动和上下拖动)
前言 本例是在React中实现,不过改一改通过原生js也很好实现,另外兼容性也做到了IE9.(IE8讲道理也是可以的). 首先看一下需要实现的需求: 要拖动图中的白色横条调整绿色和蓝色区域的高度,要拖 ...
- 设置placeholder无效解决办法
一.设置placeholder的方法 placeholder属性用来设置控件内部的提示信息 <input type="text" placeholder="请输入用 ...
- Linux 下RPM打包制作流程
原文地址:https://www.cnblogs.com/postgres/p/5726339.html 开始前的准备 安装rpmbuild软件包 yum -y install rpm-build 生 ...
- vue2.0之axios使用详解
axios 基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中使用 功能特性 在浏览器中发送 XMLHttpRequests 请求 在 node.js 中发送 htt ...
- Array Division CodeForces - 808D (构造+实现)
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into t ...
- openstack-KVM-Network
一.网络配置 1.查看网卡信息: lspci | grep Ethernet ethtool -i eth0 (qemu) info network virsh qemu-monitor-comman ...
- php-fpm-运行原理(转)
转载自https://studygolang.com/articles/15073 php-fpm是一种master(主)/worker(子)多进程架构,与nginx设计风格有点类似.master进程 ...
- centos6.7用yum安装redis解决办法及IP限制配置
在centos6.7用yum安装redis解决办法 - bluesky1 - 博客园 http://www.cnblogs.com/lanblogs/p/6104834.html yum instal ...