题目链接:http://poj.org/problem?id=1386

思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指向尾字母的有向边,

则则所有的单词构成了一个有向图,问题变为判断该有向图中是否存在一条欧拉路;有向图中存在欧拉路的两个充分必要条件为:该图是联通的并且所有的点的入度等于出度或者只存在两个点的入度与出度不等,一个点的入度=出度+1,另一个点的入度=出度-1;

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; const int MAX_N = ;
const int MAX_M = + ;
int fa[MAX_N], show[MAX_N];
int in[MAX_N], out[MAX_N];
int set_visited[MAX_N];
char str[MAX_M]; void Init()
{
for (int i = ; i < MAX_N; ++i)
fa[i] = i;
memset(in, , sizeof(in));
memset(out, , sizeof(out));
memset(show, , sizeof(show));
memset(set_visited, , sizeof(set_visited));
} int Find(int a)
{
if (fa[a] == a)
return a;
else
return fa[a] = Find(fa[a]);
} int Union(int a, int b)
{
int fa_a = Find(a);
int fa_b = Find(b); if (fa_a == fa_b)
return -;
if (fa_a > fa_b)
fa[fa_b] = fa_a;
else
fa[fa_a] = fa_b;
return ;
} int main()
{
int case_times, n, len; scanf("%d", &case_times);
while (case_times--)
{
scanf("%d", &n);
Init();
for (int i = ; i < n; ++i)
{
int l, r; scanf("%s", str);
len = strlen(str);
l = str[] - 'a';
r = str[len - ] - 'a';
in[l]++;
out[r]++;
show[l] = show[r] = ;
Union(l, r);
}
int set_count = ;
int not_equal = ;
int in_big_out = , out_big_in = ;
bool ok = true;
for (int i = ; i < MAX_N; ++i)
{
if (show[i])
{
int fa_i = Find(i);
if (set_visited[fa_i] == )
{
set_count++;
set_visited[fa_i] = ;
}
if (set_count > )
{
ok = false;
break;
}
if (in[i] != out[i])
not_equal++;
if (in[i] == out[i] + )
in_big_out++;
if (in[i] + == out[i])
out_big_in++;
}
} if (!((not_equal == ) || (out_big_in == && in_big_out ==
&& in_big_out + out_big_in == not_equal)))
ok = false; if (ok)
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
return ;
}

poj 1386 Play on Words(有向图欧拉路+并查集)的更多相关文章

  1. POJ 1386 Play on Words(欧拉路)

    http://poj.org/problem?id=1386 题意: 给出多个单词,只有单词首字母与上一个单子的末尾字母相同时可以连接,判断所有字母是否可以全部连接在一起. 思路: 判断是否存在欧拉道 ...

  2. poj 1386 Play on Words(有向图欧拉回路)

    /* 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 再次强调有向图欧拉路或欧拉回路的判 ...

  3. Play on Words(有向图欧拉路)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8571   Accepted: 2997 Description Some ...

  4. POJ1386Play on Words[有向图欧拉路]

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

  5. 【POJ 1988】 Cube Stacking (带权并查集)

    Cube Stacking Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)id ...

  6. 【POJ 1984】Navigation Nightmare(带权并查集)

    Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...

  7. POJ 1703 Find them, Catch them (数据结构-并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31102   Accepted: ...

  8. POJ 1984 Navigation Nightmare 【经典带权并查集】

    任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K To ...

  9. [poj 2912] Rochambeau 解题报告 (带权并查集)

    题目链接:http://poj.org/problem?id=2912 题目: 题目大意: n个人进行m轮剪刀石头布游戏(0<n<=500,0<=m<=2000) 接下来m行形 ...

随机推荐

  1. [C#参考]利用Socket连续发送数据

    这个例子只是一个简单的连续发送数据,接收数据的DEMO.因为最近做一个项目,要求robot连续的通过Socket传回自己的当前的位置坐标,然后客户端接收到坐标信息,在本地绘制地图,实时显示robot的 ...

  2. Java Web 部署到Tomcat

    1.在conf目录中,新建Catalina(注意大小写)\localhost目录,在该目录中新建一个xml文件,名字可以随意取,只要和当前文件中的文件名不重复就行了,该xml文件的内容为: <C ...

  3. 关于iPhone

    ---------------------- 美版有三个版本 A S V A版不能用电信卡,S不能发短信 据说还可能再次上锁 V版目前是大家认为最安全的版本 价格也是比A和S贵的 港版比V版唯一的好处 ...

  4. 四轴飞行器1.6 emwin与ucgui的移植,汉字外挂字库移植和DEMO效果对比

    飞控的遥控器打算自己做,这样全局都能掌握,可以通过遥控器对飞控的参数和飞行模式进行修改,而买遥控器是做不到这样的哈..以后做图传的时候,屏幕还可以实时现实摄像头拍回来的画面,挺好的哈.. 做遥控我们选 ...

  5. (Problem 19)Counting Sundays

    You are given the following information, but you may prefer to do some research for yourself. 1 Jan ...

  6. The error indicates that IIS is in 32 bit mode, while this application is a 64 b it application and thus not compatible.

    I was trying to install a new WSS v3 Sharepoint on a 64 bit Windows 2003 server today but the instal ...

  7. 使用 Java 实现 Comet 风格的 Web 应用

    参考这个: http://www.ibm.com/developerworks/cn/web/wa-cometjava/

  8. Intellij IDEA + Android SDK + Genymotion Emulator打造最佳Android开发

    原文:Intellij IDEA + Android SDK + Genymotion Emulator打造最佳Android开发 Intellij IDEA + Android SDK + Geny ...

  9. android ListView的上部下拉刷新下部点击加载更多具体实现及拓展

    android ListView的上部下拉刷新下部点击加载更多具体实现及拓展 ListView下拉刷新,上拉自动加载更多 下拉刷新以及加载更多

  10. Tomcat远程调试和加入JMS(转)

    1.Tomcat 加入远程调试,在catalina.bat : SET CATALINA_OPTS=-server -Xdebug -Xnoagent -Djava.compiler=NONE -Xr ...