POJ 2337 输出欧拉路径
太无语了。
这道题做了一整天。
主要还是我太弱了。
以后这个就当输出欧拉路径的模版吧。
题目中的输出字典序最小我有点搞不清楚,看了别人是这么写的。但是我发现我过不了后面DISCUSS里面的数据。
题意理解问题还是题目问题?
这道题大致以下分几步吧。
判断图是否连通,用并查集判断即可。
判断图是否有欧拉回路或者通路,判断出度和入度即可,若是欧拉通路,找出起点。
DFS找出欧拉路径输出。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#include <iomanip>
#define PI acos(-1.0)
#define Max 2505
#define inf 1<<28
#define LL(x) ( x << 1 )
#define RR(x) ( x << 1 | 1 )
#define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i )
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)
#define PII pair<int,int>
#define N 10005
using namespace std; inline void RD(int &ret) {
char c;
do {
c = getchar();
} while(c < '0' || c > '9') ;
ret = c - '0';
while((c=getchar()) >= '0' && c <= '9')
ret = ret * 10 + ( c - '0' );
}
inline void OT(int a) {
if(a >= 10)OT(a / 10) ;
putchar(a % 10 + '0') ;
} char a[N] ;
vector<string>g[300] ;
int in[N] ,out[N] ;
int vis[N] ;
int f[N] ;
int n ;
int first ; int find(int x) {
return f[x] == x ? x : f[x] = find(f[x]) ;
} void Union(int a ,int b) {
a = find(a) ;
b = find(b) ;
if(a == b)return ;
if(a > b)f[a] = b ;
else f[b] = a ;
} void init() {
mem(in , 0) ;
mem(out ,0) ;
for (int i = 0 ; i < 260 ; i ++ )f[i] = i ;
for (int i = 0 ; i < 260 ; i ++ )g[i].clear() ;
first = 0 ;
mem(vis, 0) ;
} stack<string>ans ;
void dfs(int now ){
int sz = g[now].size() ;
while(g[now].size() > 0){
string nn = g[now][0] ;
int tt = g[now][0][g[now][0].size() - 1] - 'a' ;
g[now].erase(g[now].begin()) ;
dfs(tt) ;
ans.push(nn) ;
}
}
int main() {
int T ;
#ifndef ONLINE_JUDGE
freopen("D:\\acm.txt","r",stdin) ;
#endif
cin >> T ;
while(T -- ) {
cin >> n ;
init() ;
for (int i = 1 ; i <= n ; i ++ ) {
scanf("%s",a) ;
int s = a[0] - 'a' ;
int e = a[strlen(a) - 1] - 'a' ;
out[s] ++ ;
in[e] ++ ;
g[s].push_back(a) ;
Union(s , e) ;
vis[s] = vis[e] = 1 ;
}
int fnum = 0 ;
int dnum = 0 ;
bool flag = 0 ;
int st = -1 ;
for (int i = 0 ; i < 26 ; i ++ ) {
if(!vis[i])continue ;
if(i == find(i))fnum ++ ;
if(in[i] - out[i] == 1){
dnum ++ ;
}
if(out[i] - in[i] == 1){
dnum ++ ;
st = i ;
}
if(abs(out[i] - in[i]) >= 2){
flag = 1 ;
}
}
if((dnum == 1 || dnum > 2) ||(flag) || (fnum != 1)) {
puts("***") ;
continue ;
}
else{
for (int i = 0 ;i < 26; i ++ ){
if(st == -1 && vis[i])st = i ;//如果是欧拉回路,则找到字典序最小的起点。
sort(g[i].begin() , g[i].end()) ;
}
while(!ans.empty())ans.pop() ;
dfs(st) ;
printf("%s",ans.top().c_str()) ;
ans.pop() ;
while(!ans.empty()){
printf(".%s",ans.top().c_str()) ;
ans.pop() ;
}
puts("") ;
}
}
return 0 ;
}
POJ 2337 输出欧拉路径的更多相关文章
- poj 2337 有向图输出欧拉路径
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10186 Accepted: 2650 Descrip ...
- POJ 2337 Catenyms(有向欧拉图:输出欧拉路径)
题目链接>>>>>> 题目大意: 给出一些字符串,问能否将这些字符串 按照 词语接龙,首尾相接 的规则 使得每个字符串出现一次 如果可以 按字典序输出这个字符串 ...
- POJ 2337 Catenyms(欧拉回(通)路:路径输出+最小字典序)
题目链接:http://poj.org/problem?id=2337 题目大意:给你n个字符串,只有字符串首和尾相同才能连接起来.请你以最小字典序输出连接好的单词. 解题思路:跟POJ1386一个意 ...
- poj 2337 Catenyms 【欧拉路径】
题目链接:http://poj.org/problem?id=2337 题意:给定一些单词,假设一个单词的尾字母与还有一个的首字母同样则能够连接.问能否够每一个单词用一次,将全部单词连接,能够则输出字 ...
- POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8756 Accepted: 2306 Descript ...
- poj 2337 欧拉回路输出最小字典序路径 ***
把26个小写字母当成点,每个单词就是一条边. 然后就是求欧拉路径. #include<cstdio> #include<iostream> #include<algori ...
- Catenyms POJ - 2337(单词+字典序输出路径)
题意: 就是给出几个单词 看能否组成欧拉回路或路径 当然还是让输出组成的最小字典序的路 解析: 还是把首尾字母看成点 把单词看成边 记录边就好了 这题让我对fleury输出最小字典序又加深了一些 ...
- poj 2337(单向欧拉路的判断以及输出)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11648 Accepted: 3036 Descrip ...
- POJ 2337 Catenyms
http://poj.org/problem?id=2337 题意: 判断给出的单词能否首尾相连,输出字典序最小的欧拉路径. 思路: 因为要按字典序大小输出路径,所以先将字符串排序,这样加边的时候就会 ...
随机推荐
- Same binary weight (位运算)
题目描述 The binary weight of a positive integer is the number of 1's in its binary representation.for ...
- hdu 4831 Scenic Popularity(模拟)
pid=4831" style="font-weight:normal">题目链接:hdu 4831 Scenic Popularity 题目大意:略. 解题思路: ...
- hdu 1712 (分组背包入门)
http://acm.hdu.edu.cn/showproblem.php?pid=1712 问题 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].这些物品被划分为若干组, ...
- 从Ubuntu12.04升级到Ubuntu 14.04之后,系统将无法启动
进入Ubuntu启动界面.通常有几个选项,Ubuntu,Ubuntu先进... 输入e键,进入grub的设置界面.将里面的ro改动为rw就可以. 以上能够启动,临时性的设置 可是为了永久保存这个设置, ...
- CSharp设计模式读书笔记(7):适配器模式(学习难度:★★☆☆☆,使用频率:★★★★☆)
适配器模式(Adapter Pattern):将一个接口转换成客户希望的另一个接口,使接口不兼容的那些类可以一起工作,其别名为包装器(Wrapper).适配器模式既可以作为类结构型模式,也可以作为对象 ...
- 持续交付工具ThoughtWorks Go部署step by step
持续交付工具ThoughtWorks Go部署step by step http://blogs.360.cn/360cloud/2014/05/13/%E6%8C%81%E7%BB%AD%E4%BA ...
- SQL Server 得到SPID,唯一的sessionID
像.net中的session一样,假设能知道了数据库中的sessionID,那全部的操作都能知道了,由于有了这个唯一的身份识别的标识. 能够做的事情有非常多,如:当前哪个用户在做什么操作,在运行什么s ...
- Unofficial Microsoft SQL Server Driver for PHP (sqlsrv)非官方的PHP SQL Server 驱动
原文 Unofficial Microsoft SQL Server Driver for PHP (sqlsrv) Here are unofficial modified builds of Mi ...
- 多线程学习之二坚不可摧模式Immutable pattern
Immutable pattern[坚不可摧模式] 一:immutable pattern的参与者--->immutable(不变的)参与者 1.1:immutable参与者是一个 ...
- jmeter之自定义java请求性能测试
一.环境准备 1.新建一个java工程 2.导入jar包:ApacheJMeter_core.jar ApacheJMeter_java.jar ...