有向图是否具有欧拉通路或回路的判定:

欧拉通路:图连通;除2个端点外其余节点入度=出度;1个端点入度比出度大1;一个端点入度比出度小1 或 所有节点入度等于出度

欧拉回路:图连通;所有节点入度等于出度

#include<stdio.h>
#include<string.h>
#define MAX 27
int in[MAX],out[MAX];
int visit[MAX],father[MAX];
int find(int index)
{
if(index==father[index]) return index;
else return find(father[index]);
}
int main(void)
{
int t,n;
int i,j;
int s,e;
char str[];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(visit,,sizeof(visit));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
for(i=;i<MAX;i++) father[i]=i; for(i=;i<n;i++){
scanf("%s",str);
int len=strlen(str);
s=str[]-'a',e=str[len-]-'a';
father[s]=father[e]=find(s);
visit[s]=visit[e]=;
out[s]++;in[e]++;
}
//判断改图是否连通
int r=;
for(i=;i<MAX;i++){
if(visit[i]&&i==father[i]) r++;
}
if(r>){ //aba abc
printf("The door cannot be opened.\n"); continue;
} int x,y,z,h;
x=y=z=h=;
for(i=;i<MAX;i++){
if(visit[i]){
if(out[i]-in[i]====) x++;
else if(in[i]-out[i]==)y++;
else if(in[i]==out[i]) z++;
else h++;
}
}
if(h==&&((x==&&y==)||(x==||y==))) printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n"); }
return ;
}

HDU - 1116 Play on Words(欧拉图)的更多相关文章

  1. Play on Words HDU - 1116 (并查集 + 欧拉通路)

    Play on Words HDU - 1116 Some of the secret doors contain a very interesting word puzzle. The team o ...

  2. hdu 1116 Play on Words

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 欧拉通路和欧拉回路 #include <cstdio> #include <cstrin ...

  3. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

  4. HDU 1116 Play on Words(欧拉回路+并查集)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1116 Play on Words Time Limit: 10000/5000 MS (Java/Ot ...

  5. HDU 1116 Play on Words(欧拉路径(回路))

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 题意:判断n个单词是否可以相连成一条链或一个环,两个单词可以相连的条件是 前一个单词的最后一个字母和后一个 ...

  6. HDU 1116 Play on Words(并查集和欧拉回路)(有向图的欧拉回路)

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  7. POJ 1386&&HDU 1116 Play on Words(我以后再也不用cin啦!!!)

    Play on Words Some of the secret doors contain a very interesting word puzzle. The team of archaeolo ...

  8. HDU 1116 Play on Words(有向欧拉判断)

    题目链接 题意:给出一些单词,问全部单词能否首尾相连 直接 将每一个单词第一个和最后一个字母建立一条有向边,保证除了首尾两个出入度不相等,其他的要保证相等.还有一个条件就是 首尾两个出入度差为1 同时 ...

  9. hdu 1116 并查集和欧拉路径

    ---恢复内容开始--- 把它看成是一个图 只是需要欧拉路径就可以了 首尾能连成一条线即可 如果要判断这个图是否连通 得用并查集 在hrbust oj里面看答案学到的方法 不用各种for循环套着判断能 ...

随机推荐

  1. Bash debug

    Debugging bash scripts Bash can help us to find problems in bash scripts in some ways. You don't exp ...

  2. Qt窗口的标题栏自绘

    因个人需要,要修改Qt Widget的标题栏,网上找了大半天,没有得到答案,但发现问的人比较多 所以现将找到的此文分享一下. (原文:http://www.qtsoftware.com/develop ...

  3. IE9下报错,错误: “JSON”未定义

    今天在公司运行的代码好好的,但是拿回家里以后就报错了 结果是IE9,没有设为兼容模式,唉,微软导出都是坑啊.

  4. [LeetCode][Python]Tow Sum

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/two-sum/ Given an array of integers, find ...

  5. mac下通过xcodebuild使用oclint

    step1 :下载oclint并安装 下载地址: http://oclint.org/downloads.html 选择mac os x或者darwin的包,下载到本地. 文件夹类似以下: oclin ...

  6. Qt程序设计——txt文本中获取字符串的问题

    QT对txt文本进行读写一般是采用QFile和QTextStream结合使用,在此不细说,主要说一下读取txt文本的注意事项.因为txt文本中有中文也有英文还有数字,要准确获得需要的字串可就要多一个心 ...

  7. HDU 4914 Linear recursive sequence(矩阵乘法递推的优化)

    题解见X姐的论文 矩阵乘法递推的优化.仅仅是mark一下. .

  8. 在Prefetcher中取消robots.txt的限制

    Robots.txt是一种专门用于搜索引擎网络爬虫的文件,当构造一个网站时,如果作者希望该网站的内容被搜索引擎收录,就可以在网站中创建一个纯文本文件robots.txt,在这个文件中,声明该网站不想被 ...

  9. android入门——UI(5)

    最近时间实在匆忙,博客的代码基本没有解释. 介绍ExpandableListView <?xml version="1.0" encoding="utf-8&quo ...

  10. 怎样在UICollectionView中添加Header和footer

    ---恢复内容开始--- 怎样在UICollectionView中添加Header和footer 转载于http://my.oschina.net/zboy/blog/221525 摘要 来自-htt ...