poj 1780 code(欧拉路)
/*
对于n为密码想要序列最短 那么 1234 2345 这两个一定挨着
就是说 前一个的后n-1位是后一个的前n-1位 假设n==3
我们用0-99作为点的编号建图 然后每个点连出去10条边
两个相邻点有n-1个是重复的 边的权值可用两个点计算
比如 12 23 权值为123 123 234 权值为1234
显然最后的序列是每个边记录一次 也就是跑欧拉路
对于记录下的边权 第一条输出前n-1为 上下的输出最后一位
这就是答案了 poj上没有special judge 要求字典序最小
这里建边时从9-0 就ok了
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 1000010
using namespace std;
int n,m,num,head[maxn],s[maxn],len;
int S[maxn],top,f[maxn],T[maxn];
struct node
{
int u,v,t,pre;
}e[maxn*];
void Clear()
{
num=len=top=;
memset(head,-,sizeof(head));
memset(head,-,sizeof(head));
memset(f,,sizeof(f));
}
void Add(int from,int to,int dis)
{
e[num].v=to;
e[num].t=dis;
e[num].pre=head[from];
head[from]=num++;
}
void Dfs(int x)
{
S[++top]=x;T[top]=;
while(top)
{
int u=S[top],falg=,ti=T[top];
for(int i=head[u];i!=-;i=e[i].pre)
{
int v=e[i].v;
if(f[e[i].t])continue;
falg=;f[e[i].t]=;
S[++top]=v;T[top]=e[i].t;
break;
}
if(!falg)s[++len]=ti,top--;
}
}
int main()
{
while(scanf("%d",&n)&&n)
{
Clear();
if(n==){puts("");continue;}
int t=,r=;
for(int i=;i<n;i++)t*=,r*=;r/=;
for(int i=;i<t;i++)
for(int j=;j>=;j--)
Add(i,i%r*+j,i*+j);
Dfs();
for(int i=;i<=n-;i++)printf("");
printf("%d",s[len]/);
for(int i=len-;i>=;i--)printf("%d",s[i]%);
printf("\n");
}
}
poj 1780 code(欧拉路)的更多相关文章
- POJ 2230 (欧拉路)
分析: 基础的欧拉路算法,变化在于要求每条边正向和反向各走一遍. 链式前向星构图,只要标记走过的单向边,边找边输出即可. code #include <iostream> #include ...
- [欧拉回路+手动开栈] poj 1780 Code
题目链接: http://poj.org/problem? id=1780 Code Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- POJ 1780 Code(有向图的欧拉通路)
输入n(1<=n<=6),输出长度为10^n + n -1 的字符串答案. 其中,字符串以每n个为一组,使得所有组都互不相同,且输出的字符串要求字典序最小. 显然a[01...(n-1)] ...
- POJ 1780 Code(欧拉回路+非递归dfs)
http://poj.org/problem?id=1780 题意:有个保险箱子是n位数字编码,当正确输入最后一位编码后就会打开(即输入任意多的数字只有最后n位数字有效)……要选择一个好的数字序列,最 ...
- poj 2337(单向欧拉路的判断以及输出)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11648 Accepted: 3036 Descrip ...
- poj 1780 Code
//题目描述:KEY公司开发出一种新的保险箱.要打开保险箱,不需要钥匙,但需要输入一个正确的.由n位数字组成的编码.这种保险箱有几种类型,从给小孩子玩的玩具(2位数字编码)到军用型的保险箱(6位数字编 ...
- 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 ...
随机推荐
- Django Web在Apache上的部署
1. 安装配置Apache 2. 安装wsgi_mod模块 3. 开放相应端口 vim /etc/sysconfig/iptables # Firewall configuration written ...
- SQL联合索引 与 单一列的索引
SQL联合索引 与 单一列的索引 标签: sqlwebobjectstatistics优化磁盘 2012-06-12 13:46 27992人阅读 评论(1) 收藏 举报 分类: 数据库(94) ...
- PHP与MYSQL配合完成IP的存取
如何存储IP 程序设计要在功能实现的基础上最大限度的优化性能.而数据库设计是程序设计中不可忽略的重要部分,巧存IP地址可以一定程度提升性能. 利用函数算法处理 MySQL没有直接提供IP类型字段,但有 ...
- 【Linux】基础配置-修改命令提示符的风格
1,效果图: [groot]$ 2,设置步骤: 编辑~/.bashrc文件,在最后增加设置行: #显示当面目录的最后一层目录#PS1='\[\e[32m\][\u@\h \W]$\[\e[m\]'#只 ...
- WM_SIZE和WM_MOVE的函数体内容为什么不一样?
搞不懂,要想一想- procedure TWinControl.WMSize(var Message: TWMSize); begin UpdateBounds; // 类函数 inherited; ...
- 根据body的内容 查找h2标签的@class="subtitle"的值
<pre name="code" class="html"><body class="api jquery listing" ...
- 牛逼的bootcss之buttons
css源码 /*! @license * * Buttons * Copyright 2012-2014 Alex Wolfe and Rob Levin * * Licensed under the ...
- Balance(01背包)
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9163 Accepted: 5617 Description Gigel ...
- oracle中触发器中:new和:old 的使用方法--4
转载▼ create or replace trigger TRI_PrintTest before delete or insert or update on TEST_EXAM --触发事 ...
- LINUX怎么修改IP地址
1,先搜索了一下,得到以下解释 IP IP地址 Netmark 子网掩码 Gateway 默认网关 HostName 主机名称 DomainName 域名 DNS DNS的IP 2,需要修改的文件常有 ...