每个单词可以看做一条边,每个字母就是顶点。

有向图欧拉回路的判定,首先判断入度和出度,其实这个题判定的是欧拉通路,不一定非得构成环,所以可以有一个点的顶点入度比出度大1,另外一个点的出度比入度大1,或者每个点的出度和入度相等。用并查集判断是否弱联通。最后dfs求出欧拉路径,不过这个题是让求字典序最小的那个,所以加边之前先把边排序。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
const int maxn = ;
struct Edge {
int to, next;
int id;
}edge[maxn * ];
int tot, head[maxn];
int in[maxn], out[maxn];
int F[maxn];
int st;
bool vis[];
string str[maxn];
void init()
{
tot = ;
memset(head, -, sizeof(head));
memset(F, -, sizeof(F));
}
void addedge(int u, int v, int id)
{
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].id = id;
head[u] = tot++;
}
int Find(int x)
{
if (F[x] == -) return x;
return F[x] = Find(F[x]);
}
void Union(int x, int y)
{
int tx = Find(x);
int ty = Find(y);
if (tx != ty)
F[tx] = ty;
}
bool check(int s)
{
int in1 = , out1 = ;
for (int i = ; i <= ; i++)//判断出入度关系
{
if (in[i] == out[i]) continue;
else if (in[i] - out[i] == ) in1++;
else if (out[i] - in[i] == ) out1++, st = i;//如果有出度比入度大1的,说明是欧拉通路,起点只能是那个出度比入度大1的那个点
else return false;
}
//printf("in1 = %d, out1 = %d\n", in1, out1);
if (!(in1 == && out1 == ) && !(in1 == && out1 == )) return false;
for (int i = ; i <= ; i++)//判断弱联通
if (vis[i] && Find(i) != Find(s))
return false;
return true;
}
bool vis2[maxn * ];//判断每条边是否访问过。
int top;
int ans[maxn * ];//保存路径
void dfs(int u)
{
for (int i = head[u]; i != -; i = edge[i].next)
{
if (!vis2[i])
{
vis2[i] = true;
dfs(edge[i].to);
ans[top++] = i;
}
}
}
int main()
{
int T, n;
scanf("%d", &T);
while (T--)
{
init();
scanf("%d", &n);
memset(in, , sizeof(in));
memset(out, , sizeof(out));
memset(vis, false, sizeof(vis));
int u, v;
st = ;
for (int i = ; i <n; i++)
cin >> str[i];
sort(str, str + n);//从小到大排序
for (int i = n - ; i >= ; i--)//因为链式前向星是逆序存图,所以反过来从大到小读入。
{
u = str[i][] - 'a' + ;
v = str[i][str[i].length() - ] - 'a' + ;
vis[u] = vis[v] = true;
addedge(u, v, i);
++in[v];
++out[u];
Union(u, v);
st = min(st, min(u, v));//找出最小的那个点来
}
if (!check(st))
puts("***");
else
{
top = ;
memset(vis2, false, sizeof(vis2));
dfs(st);
for (int i = top - ; i > ; i--)
cout << str[edge[ans[i]].id] << ".";
cout << str[edge[ans[]].id] << endl;
} }
return ;
}

有向图的欧拉路径POJ2337的更多相关文章

  1. poj 2337 有向图输出欧拉路径

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10186   Accepted: 2650 Descrip ...

  2. poj2337 欧拉路径

    poj2337 这道题昨天晚上开始做,今天才A.但是问题想透了, 发现其实没那么难 题目大意: 给你一些单词,如果一个单词的末尾字符与另一个单词首字符相同,则两个的单词可以连接.问是否可以把所有单词连 ...

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

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

  4. Play on Words UVA - 10129 欧拉路径

    关于欧拉回路和欧拉路径 定义:欧拉回路:每条边恰好只走一次,并能回到出发点的路径欧拉路径:经过每一条边一次,但是不要求回到起始点 ①首先看欧拉回路存在性的判定: 一.无向图每个顶点的度数都是偶数,则存 ...

  5. POJ 1637 混合图的欧拉回路判定

    题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定 ...

  6. POJ 1637 Sightseeing tour (混合图欧拉路判定)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6986   Accepted: 2901 ...

  7. SPOJ Play on Words

    传送门 WORDS1 - Play on Words #graph-theory #euler-circuit Some of the secret doors contain a very inte ...

  8. Sightseeing tour

    Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8276 Accepted: 3489 Desc ...

  9. POJ 1637 - Sightseeing tour - [最大流解决混合图欧拉回路]

    嗯,这是我上一篇文章说的那本宝典的第二题,我只想说,真TM是本宝典……做的我又痛苦又激动……(我感觉ACM的日常尽在这张表情中了) 题目链接:http://poj.org/problem?id=163 ...

随机推荐

  1. 关于在DEDECMS当中模板文件不存在的解决方案

    大家可能在生成文档的时候遇到过,模板文件不存在,无法解析  这个问题,其实这个遇到这个问题的大多数人应该是修改了默认模板的名称才导致这样的问题,如果你避免这种问题大家在一开始对模板进行命名的时候就要写 ...

  2. Unable to find the ncurses libraries or the required header files解决

    问题: 解决方法: sudo apt-get install ncurses-dev 参考:Unable to find the ncurses libraries or the required h ...

  3. BZOJ 1208 宠物收养所

    Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...

  4. 利用CSP探测网站登陆状态

    0x00 背景 今天看到zone里有同学发帖说了探测支付宝登录状态的帖子:http://zone.wooyun.org/content/17665 由此我想到了我们parsec的@/fd 半年前提到的 ...

  5. hdu 5063 Operation the Sequence

    http://acm.hdu.edu.cn/showproblem.php?pid=5063 思路:因为3查询最多50,所以可以在查询的时候逆操作找到原来的位置,然后再求查询的值. #include ...

  6. AES加密跨平台出现的问题

    javax.crypto.BadPaddingException: Given final block not properly padded at com.sun.crypto.provider.S ...

  7. QT、QTE、qtopia区别

    QT.QTE.qtopia区别 Qt的授权是分为两条线,商业版和开源版.如果使用商业版的Qt,那么开发出的程序可以是私有的和商业的:如果使用的是开源版的Qt,由于其使用的是GPL协议,那么可发出的程序 ...

  8. UVA-11983-Weird Advertisement(线段树+扫描线)[求矩形覆盖K次以上的面积]

    题意: 求矩形覆盖K次以上的面积 分析: k很小,可以开K颗线段树,用sum[rt][i]来保存覆盖i次的区间和,K次以上全算K次 // File Name: 11983.cpp // Author: ...

  9. LightOJ 1135(线段树)

    题解引自:http://www.cnblogs.com/wuyiqi/archive/2012/05/27/2520642.html 题意: 有n个数,刚开始都为0 add i , j 给i,j区间内 ...

  10. ♫【JS基础】壹零零壹

    如何面试一个前端开发者? function spacify(str) { return str.split('').join(' ') } console.log(spacify('hello wor ...