http://acm.hdu.edu.cn/showproblem.php?pid=1116

 #include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
const int N=;
int father[N],vis[N];
int find(int x)//查找根
{
if(father[x]!=x)
father[x]=find(father[x]);
return father[x];
}
void make(int a,int b)//合并操作
{
int f1=find(a);
int f2=find(b);
if(f1!=f2)
father[f1]=f2;
} int main()
{
//freopen("in.txt","r",stdin);
int t;
cin>>t;
while(t--)
{
int out[N],in[N],p[N];
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(vis,,sizeof(vis));
int n;
for(int i=;i<;i++)
father[i]=i;//数组赋初值
cin >> n;
while(n--)
{
char str[];
cin>>str;
int a=str[]-'a';
int b=str[strlen(str)-]-'a';
out[a]++;
in[b]++;
make(a,b);//合并操作
vis[a]=vis[b]=;
}
for(int i=;i<;i++)
father[i]=find(i);//寻找每个点的根节点
int cnt=;
for(int i=;i<;i++)
{
if(vis[i] && father[i]==i)//确定有几颗树
cnt++;
}
if(cnt>)
{
printf("The door cannot be opened.\n");
continue;
}
int j=;
for(int i=;i<;i++)
{
if(vis[i] && out[i]!=in[i])//找起点和终点
p[j++]=i;
}
if(j==)//环
{
printf("Ordering is possible.\n");
continue;
}
if(j== && ((out[p[]]-in[p[]]== && in[p[]]-out[p[]]== ) ||
(in[p[]]-out[p[]]== && out[p[]]-in[p[]]== ) ) )
{//起点和终点的判断
printf("Ordering is possible.\n");
continue;
}
printf("The door cannot be opened.\n");
}
return ;
}

hdu1116的更多相关文章

  1. HDU1116 Play on Words(有向图欧拉通路)

    我把单词当作点,然后这样其实是不对的,这样就要判定是否是哈密顿通路.. 这题应该把单词的首尾单词当作点,而单词本身就是边,那样就是判定欧拉通路了. 有向图包含欧拉通路的充要条件是:首先基图连通,然后是 ...

  2. Play on Words[HDU1116]

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

  3. hdu1116 欧拉回路

    //Accepted 248 KB 125 ms //欧拉回路 //以26个字母为定点,一个单词为从首字母到末尾字母的一条边 //下面就是有向图判断欧拉回路 //连通+节点入度和==出度和 或者 存在 ...

  4. HDU1116图论

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1116 #include<stdio.h> #include<algorithm&g ...

  5. hdu1116回溯N皇后问题

    题目连接 经过思考,不难发现:恰好N个皇后放在不同行不同列,那么是不是可以转换成N个皇后所在行分别确定(一人一行)的情况下对她们的所在列的枚举. 也就是列的全排列生成问题,我们用c[x]表示x行皇后的 ...

  6. hdu1116有向图判断欧拉通路判断

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

  7. hdu-1116(欧拉回路+并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1116 思路:将字符串的头元素和尾元素视为图的x,y节点,然后合并x,y. 如果这个图不连通,则门不能打 ...

  8. HDU1116(欧拉路径+并查集)

    题意: 给出一些字符串,有这两个字符串,如果第一个字符串的最后一个字母和第二个字符串的第一个字母是一样的,则这两个字符串是可以连接在一起的. 问给出的这些字符串能否串成一个环或者一整个链. 思路: 将 ...

  9. hdu1116 Play on Words--并查集

    原题链接: pid=1116">http://acm.hdu.edu.cn/showproblem.php? pid=1116 一:原题内容 Problem Description S ...

随机推荐

  1. ant条件逻辑

    <condition property="sdk-folder" value="E:\android\android-sdk\adt-bundle-windows- ...

  2. VS2010恢复默认编辑环境的设置

    VS2010恢复默认编辑环境的设置 VS2010在安装完成后初次打开的时候可以设置自己常用的环境为默认打开的编辑环境, 也可以在打开IDE以后通过如下步骤设置默认环境: Tools->Impor ...

  3. “==”,比较的是引用 “equals方法”比较的是具体内容

    package com.java1234.chap03.sec08; public class Demo2 { public static void main(String[] args) { //“ ...

  4. gprof

    函数级耗时剖析.gprof不会说谎,仔细考虑下函数的实现细节. 准备工作 对单文件simulator.cpp编译 编译:g++ -g -pg simulator.cpp -o simulator.o ...

  5. 正则匹配 去掉 多余的js和html标签

    $reg17 = '/><strong>公司介绍<\/strong><\/td>([\S\s*]+?)<\/div>/'; $this->d ...

  6. 记录sublime text2的技巧

    好吧,其实俺是sublime text控,用了那么的编辑器,从最初的notepad++,后来到Dreawaver,现在只钟情于sublime text2....记录一些比较实用的技巧和网站吧!! 方便 ...

  7. struct和class区别

    转载来源:http://blog.sina.com.cn/s/blog_48f587a80100k630.html C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据 ...

  8. Memcached 配置 和项目应用

    Memcached 配置 http://blog.csdn.net/sup_heaven/article/details/32337711 memcached真实项目中的应用 http://blog. ...

  9. Win7下MongoDB安装

    一.下载MongoDB 下载地址:http://www.mongodb.org/downloads 注意:1.从2.2开始,MongoDB不再支持windows xp. 2.32位MongoDB最大支 ...

  10. Freemarker例子

    1.引入架包 2.写ftl文件 3.代码 hello.ftl 你好啊,${hello},今天你的精神不错! if else 语句测试 <#if num gt 18><#-- 不使用 ...