POJ2337 Catenyms(欧拉通路的求解)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11526 | Accepted: 2993 |
Description
dog.gopher
gopher.rat
rat.tiger
aloha.aloha
arachnid.dog
A compound catenym is a sequence of three or more words separated by periods such that each adjacent pair of words forms a catenym. For example,
aloha.aloha.arachnid.dog.gopher.rat.tiger
Given a dictionary of lower case words, you are to find a compound catenym that contains each of the words exactly once.
Input
Output
Sample Input
2
6
aloha
arachnid
dog
gopher
rat
tiger
3
oak
maple
elm
Sample Output
aloha.arachnid.dog.gopher.rat.tiger
***
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
const int M=;
ll power(ll a,int b,ll c) {
ll ans=;
while(b) {
if(b%==) {
ans=(ans*a)%c;
b--;
}
b/=;
a=a*a%c;
}
return ans;
}
struct Word { int l; char s[]; };
struct Edge { int st, ed; bool del; }; Word word[];
Edge edge[];
int in[], out[];
int stk[], father[];
bool mark[];
int E, top; int cmp ( const void* a, const void* b )
{
return strcmp( ((Word*)a)->s, ((Word*)b)->s );
} int find_set ( int x )
{
if ( father[x] != x )
father[x] = find_set ( father[x] );
return father[x];
} bool judge ()
{
int t = ;
for ( int i = ; i < ; i++ )
if ( mark[i] && father[i] == i ) t++;
return t == ;
} void find_path ( int u )
{
for ( int i = ; i < E; i++ )
{
if ( ! edge[i].del && edge[i].st == u )
{
edge[i].del = true;
find_path ( edge[i].ed );
stk[top++] = i;
}
}
} int main()
{
int cs;
scanf("%d",&cs);
while ( cs-- )
{
scanf("%d",&E);
int u, v, c1, c2, start, i; for ( i = ; i < ; i++ )
{
in[i] = out[i] = ;
father[i] = i;
mark[i] = false;
} for ( i = ; i < E; i++ )
{
scanf("%s",word[i].s);
word[i].l = strlen(word[i].s);
} qsort(word, E, sizeof(word[]), cmp); for ( i = ; i < E; i++ )
{
u = word[i].s[] - 'a';
v = word[i].s[word[i].l-] - 'a';
edge[i].st = u;
edge[i].ed = v;
edge[i].del = false;
mark[u] = mark[v] = true;
out[u]++; in[v]++;
u = find_set ( u );
v = find_set ( v );
if ( u != v ) father[v] = u;
} c1 = c2 = ;
start = edge[].st;
for ( i = ; i < ; i++ )
{
if ( in[i] == out[i] ) continue;
else if ( in[i] - == out[i] ) c1++;
else if ( out[i] - == in[i] ) { c2++; start = i; }
else break;
} if ( i == && ((c1 == c2 && c1 == ) || (c1 == c2 && c1 == )) && judge() )
{
top = ;
find_path ( start );
for ( i = top - ; i > ; i-- )
printf("%s.",word[stk[i]].s);
printf("%s\n",word[stk[]].s);
}
else printf("***\n");
}
//system("pause");
return ;
}
POJ2337 Catenyms(欧拉通路的求解)的更多相关文章
- ACM/ICPC 之 DFS求解欧拉通路路径(POJ2337)
判断是欧拉通路后,DFS简单剪枝求解字典序最小的欧拉通路路径 //Time:16Ms Memory:228K #include<iostream> #include<cstring& ...
- POJ 1300 欧拉通路&欧拉回路
系统的学习一遍图论!从这篇博客开始! 先介绍一些概念. 无向图: G为连通的无向图,称经过G的每条边一次并且仅一次的路径为欧拉通路. 如果欧拉通路是回路(起点和终点相同),则称此回路为欧拉回路. 具有 ...
- poj 2513 连接火柴 字典树+欧拉通路 好题
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27134 Accepted: 7186 ...
- poj2513- Colored Sticks 字典树+欧拉通路判断
题目链接:http://poj.org/problem?id=2513 思路很容易想到就是判断欧拉通路 预处理时用字典树将每个单词和数字对应即可 刚开始在并查集处理的时候出错了 代码: #includ ...
- hdu1116有向图判断欧拉通路判断
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash
题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点 或者 ...
- 欧拉回路&欧拉通路判断
欧拉回路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次, 称这条回路为欧拉回路.具有欧拉回路的图成为欧拉图. 判断欧拉通路是否存在的方法 ...
- POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)
Colored Sticks Time Limit: 5000MS Memory ...
- HDU 5883 F - The Best Path 欧拉通路 & 欧拉回路
给定一个图,要求选一个点作为起点,然后经过每条边一次,然后把访问过的点异或起来(访问一次就异或一次),然后求最大值. 首先为什么会有最大值这样的分类?就是因为你开始点选择不同,欧拉回路的结果不同,因为 ...
随机推荐
- SSTI注入绕过(沙盒逃逸原理一样)
在python沙盒逃逸中绕过道理是一样的. 1.python沙盒中删除了很多模块,但是没有删除reload reload(__builtins__),重新加载被删除的模块,直接命令执行,只用于py2 ...
- 最小化安装Linux的常用配置整理
基于安全性考虑,将服务器进行最小化安装,毕竟软件包越少,漏洞越少,相对来说就约安全,但是最小化安装会给运维带来一些问题和不便,下面是我总结的,常见的一些配置和工具的安装,仅供各位大神参考,如有新的id ...
- python学习总结---文件操作
# 文件操作 ### 目录管理(os) - 示例 ```python # 执行系统命令 # 清屏 # os.system('cls') # 调出计算器 # os.system('calc') # 查看 ...
- Kd-Tree&Ransac笔记
关于sift资源总结: http://blog.csdn.net/masibuaa/article/details/9191309 两个比较好的资源: https://my.oschina.net/k ...
- ftrace 简介
ftrace 简介 ftrace 的作用是帮助开发人员了解 Linux 内核的运行时行为,以便进行故障调试或性能分析. 最早 ftrace 是一个 function tracer,仅能够记录内核的函数 ...
- 创建虚拟机流程nova
这篇博文借鉴于http://www.cnblogs.com/yjbjingcha/p/6977741.html,感谢博友提供. 本文试图具体地描写叙述openstack创建虚拟机的完整过程.从用户发起 ...
- 一些需要注意的ts
写了一段时间ts,在从头学习一遍,温故而之新 ts的一些技巧 1.巧用注释 通过/** */形式的注释可以给 TS 类型做标记,编辑器会有更好的提示: /** A cool guy. */ inter ...
- Codeforces Round #421 (Div. 2) D. Mister B and PR Shifts
Codeforces Round #421 (Div. 2) D. Mister B and PR Shifts 题意:给一个长度为\(n\)的排列,每次可以向右循环移位一次,计算\(\sum_{i= ...
- ACM童年生活二三事
描述 Redraiment小时候走路喜欢蹦蹦跳跳,他最喜欢在楼梯上跳来跳去. 但年幼的他一次只能走上一阶或者一下子蹦上两阶. 现在一共有N阶台阶,请你计算一下Redraiment从第0阶到第N阶共有几 ...
- 为IE和chrome编写单独的样式
flex布局在IE10上不支持,为IE10编写特定的样式可以用判断navigator.userAgent 的方法 var doc = document.documentElement; doc.set ...