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 欧拉通路 & 欧拉回路
给定一个图,要求选一个点作为起点,然后经过每条边一次,然后把访问过的点异或起来(访问一次就异或一次),然后求最大值. 首先为什么会有最大值这样的分类?就是因为你开始点选择不同,欧拉回路的结果不同,因为 ...
随机推荐
- TIDB介绍
TiDB 是什么? TiDB 是一个分布式 NewSQL 数据库.它支持水平弹性扩展.ACID 事务.标准 SQL.MySQL 语法和 MySQL 协议,具有数据强一致的高可用特性,是一个不仅适合 O ...
- python import模块熟悉方法
python import导入的可用模块很多,新增速度较快,无法一次性全部掌握. 掌握熟悉一种模块的方法是非常有价值的技能. 探究模块可以从python解释器入手,具体流程记录如下: 以os模块为例: ...
- Python数据分析-Matplotlib图标绘制
Matplotlib介绍 Matplotlib是一个强大的Python绘图和数据可视化的工具包. Matplotlib的主要功能 Matplotlib是python中的一个包,主要用于绘制2D图形(当 ...
- C#学习笔记----静态字段和静态方法
1.使用关键字 static 修饰的字段或方法成为静态字段和静态方法,如 public static int num = 1;2.静态字段属于类,并为类所用.而非静态字段属于对象,只能被特定的对象专有 ...
- BZOJ 1731: [Usaco2005 dec]Layout 排队布局
Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...
- Codeforces Round #418 (Div. 2) D. An overnight dance in discotheque
Codeforces Round #418 (Div. 2) D. An overnight dance in discotheque 题意: 给\(n(n <= 1000)\)个圆,圆与圆之间 ...
- hdu 3717 二分+队列维护
思路:已知当前的总长度和为len,当前的伤害为sum,伤害次数为 num.那么对下一个点的伤害值sum=sum+2*len+num: 这个是通过(x+1)^2展开化简就能得到. #include< ...
- 牛客 NOIp模拟1 T3 保护 解题报告
保护 题目描述 \(C\)国有\(n\)个城市,城市间通过一个树形结构形成一个连通图.城市编号为\(1\)到\(n\),其中\(1\)号城市为首都.国家有\(m\)支军队,分别守卫一条路径的城市.具体 ...
- 【CF edu 27 G. Shortest Path Problem?】
time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standa ...
- navicat for mysql 2003错误
最近又一次碰到navicat for mysql 2003错误,之前碰到好几次了,问题再次出现还是有些不知所措,所以记下来,再次遇到时就可以迎韧有余了. 解决方案1: 重启防火墙:关闭再重新开启. 解 ...