poj 2337(单向欧拉路的判断以及输出)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 11648 | Accepted: 3036 |
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
*** 题意:给定一系列字符串,如果某个字符串的结尾和另外一个字符串的开头相等,那么两个字符串就可以拼接在一起,现在问所有的字符串能否刚好都出现一次?如果存在,输出字典序最小的那个组合. 题解:将字符串看成一条边,然后将其起始字符和结尾字符看成边的两个端点,构造一个有向图,然后就判断这个图是否为欧拉图了.判断单向欧拉图的方法:
有向欧拉通路:起点:出度-入度=1,终点:入度-出度=1,其它点:入度==出度
有向欧拉回路:所有点:入度==出度
还要利用并查集判断一下这个图是否只有一个连通分量.
然后将所有的边按照字典序排序,找到起点,进行DFS(Edge &e = edge[u][i] 这里检查了半天,一点要记得是地址啊...),即可得到答案.
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <stack>
using namespace std;
const int N = ;
char str[];
struct Edge{
char str[];
int to,del;
}; typedef vector <Edge> vec;
stack <string> ans;
vec edge[N];
int in[N],out[N],father[N];
bool vis[N],mark[];
int n;
void init(){
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(vis,false,sizeof(vis));
for(int i=;i<=;i++){
mark[i] = false;
edge[i].clear();
father[i] = i;
}
}
int _find(int x){
return x==father[x]?x:father[x] = _find(father[x]);
} void dfs(int u){
for(int i=;i<edge[u].size();i++){
Edge &e = edge[u][i]; ///这里要取地址
int v = e.to;
if(!e.del){
e.del = ;
dfs(v);
ans.push(e.str);
}
}
}
bool cmp(Edge a,Edge b){
return strcmp(a.str,b.str)<;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
init();
scanf("%d",&n);
int S = N;
for(int i=;i<=n;i++){
scanf("%s",str);
int s = str[]-'a'+;
int t = str[strlen(str)-]-'a'+;
Edge e;
e.del = ;
strcpy(e.str,str);
e.to = t;
edge[s].push_back(e);
in[t]++;
out[s]++;
mark[t] = mark[s] = ;
int u = _find(s);
int v = _find(t);
if(u!=v) father[u] = v;
S = min(S,min(s,t));
}
int num1 = ,num2 = ,num3 = ,num4 = ;
for(int i=;i<=;i++){
if(mark[i]==&&father[i]==i) num4++;
if(out[i]-in[i]==){
S = i;
num1++;
}
else if(in[i]-out[i]==){
num2++;
}else if(in[i]-out[i]!=){
num3++;
}
if(!edge[i].empty())
sort(edge[i].begin(),edge[i].end(),cmp);
}
if(num3||!(num1==&&num2==)&&!(num1==&&num2==)||num4>){
printf("***\n");
continue;
}
dfs(S);
int flag = ;
while(!ans.empty()){
if(flag) printf(".");
cout<<ans.top();
flag = ;
ans.pop();
}
printf("\n");
}
}
poj 2337(单向欧拉路的判断以及输出)的更多相关文章
- POJ 2230 (欧拉路)
分析: 基础的欧拉路算法,变化在于要求每条边正向和反向各走一遍. 链式前向星构图,只要标记走过的单向边,边找边输出即可. code #include <iostream> #include ...
- poj 2337 && zoj 1919 欧拉回路+连通性判断
题目要求按字典序排列,而且可能有重边 所以一开始就将数组从大到小排列,那么我将字符串加入链表时就会令小的不断前移,大的被挤到后面 这里有一点问题就是我一开始使用的是qsort: int cmp(con ...
- 欧拉路&&欧拉回路 概念及其练习
欧拉路: 如果给定无孤立结点图G,若存在一条路,经过图中每边一次且仅一次,这条路称为欧拉路: 如果给定无孤立结点图G,若存在一条回路,经过图中每边一次且仅一次,那么该回路称为欧拉回路. 存在欧拉回路的 ...
- Play on Words(欧拉路)
http://poj.org/problem?id=1386 题意:给定若干个单词,若前一个的尾字母和后一个单词的首字母相同,则这两个单词可以连接,问是否所有的单词都能连接起来. 思路:欧拉路的判断, ...
- UVA - 10129Play on Words(欧拉路)
UVA - 10129Play on Words Some of the secret doors contain a very interesting word puzzle. The team o ...
- POJ 2513 trie树+并查集判断无向图的欧拉路
生无可恋 查RE查了一个多小时.. 原因是我N define的是250500 应该是500500!!!!!!!!! 身败名裂,已无颜面对众人.. 吐槽完了 我们来说思路... 思路: 判有向图能否形成 ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- poj 1386 Play on Words(有向图欧拉路+并查集)
题目链接:http://poj.org/problem?id=1386 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指 ...
- POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]
题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...
随机推荐
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- Zend Hash table 详解--转
原文地址:http://www.phppan.com/2009/12/zend-hashtable/ 在PHP的Zend引擎中,有一个数据结构非常重要,它无处不在,是PHP数据存储的核心,各种常量.变 ...
- js new date()说明
javaScript UTC() 方法: UTC() 方法可根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数. 要创建一个一个日期对象,可以使用以下的方式: var now=new D ...
- 徒手创建一个 jsp 项目
在开始之前,先回顾一下 jsp 和 servlet,jsp 和 servlet 本质是一样的,因为 jsp 最终必须编译成 servlet 才能运行. 因为 jsp 的那些标签 jvm 是无法直接运行 ...
- Nginx报错 nginx: [error] open() "/usr/local/nginx-1.6.3/logs/nginx.pid" failed (2: No such file or directory)
问题: 解决: http://www.jianshu.com/p/918eb337a206 dd
- Leetcode 7. 整数反转(待整理)
1.题目描述 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: ...
- spring boot 2.0.3+spring cloud (Finchley)5、路由网关Spring Cloud Zuul
Zuul作为微服务系统的网关组件,用于构建边界服务,致力于动态路由.过滤.监控.弹性伸缩和安全. 为什么需要Zuul Zuul.Ribbon以及Eureka结合可以实现智能路由和负载均衡的功能:网关将 ...
- CSS3实现鼠标hover的过渡效果
我想让鼠标放在div上就让它旋转变大,离开div后它又恢复本来的样子. 于是我就想写一个JS,监听一个hover事件,当hover发生的时候,触发一个计时器,在计时器里写两个值,一个管角度,一个管宽度 ...
- 【SRM20】数学场
第一题 n个m位二进制,求异或值域总和. [题解]异或值域--->使用线性基,解决去重问题. m位二进制--->拆位,每位根据01数量可以用组合数快速统计总和. #include<c ...
- bootstrap分页查询传递中文参数到后台(get方式提交)
<!--分页 --> <div style="width: 380px; margin: 0 auto; margin-top: 50px;"> <u ...