CF-62D - Wormhouse(欧拉回路)
来源:互联网 发布:淘宝不知道3c 编辑:程序博客网 时间:2024/06/06 09:43
Description
Arnie the Worm has finished eating an apple house yet again and decided to move. He made up his mind on the plan, the way the rooms are located and how they are joined by corridors. He numbered all the rooms from 1 to n. All the corridors are bidirectional.
Arnie wants the new house to look just like the previous one. That is, it should have exactly n rooms and, if a corridor from room i to room j existed in the old house, it should be built in the new one.
We know that during the house constructing process Arnie starts to eat an apple starting from some room and only stops when he eats his way through all the corridors and returns to the starting room. It is also known that Arnie eats without stopping. That is, until Arnie finishes constructing the house, he is busy every moment of his time gnawing a new corridor. Arnie doesn't move along the already built corridors.
However, gnawing out corridors in one and the same order any time you change a house is a very difficult activity. That's why Arnie, knowing the order in which the corridors were located in the previous house, wants to gnaw corridors in another order. It is represented as a list of rooms in the order in which they should be visited. The new list should be lexicographically smallest, but it also should be strictly lexicographically greater than the previous one. Help the worm.
Input
The first line contains two integers n and m (3 ≤ n ≤ 100, 3 ≤ m ≤ 2000). It is the number of rooms and corridors in Arnie's house correspondingly. The next line contains m + 1 positive integers that do not exceed n. They are the description of Arnie's old path represented as a list of rooms he visited during the gnawing. It is guaranteed that the last number in the list coincides with the first one.
The first room described in the list is the main entrance, that's why Arnie should begin gnawing from it.
You may assume that there is no room which is connected to itself and there is at most one corridor between any pair of rooms. However, it is possible to find some isolated rooms which are disconnected from others.
Output
Print m + 1 positive integers that do not exceed n. Those numbers are the description of the new path, according to which Arnie should gnaw out his new house. If it is impossible to find new path you should print out No solution. The first number in your answer should be equal to the last one. Also it should be equal to the main entrance.
Sample Input
3 31 2 3 1
1 3 2 1
3 31 3 2 1
No solution
思路:欧拉回路遍历,找出比原路径大的最小路径。
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;const int mm=110;int g[mm][mm],chu[2100],ans[2100],n,m;bool dfs(int u,int dep,bool yes){ ans[dep]=u; if(dep==m){return yes;}///yes 前面的路径是否已经保证字典序大于原路径 int beg=chu[dep+1]; bool flag; if(yes)beg=1; for(int i=beg;i<=n;++i) if(g[u][i]) { g[u][i]=g[i][u]=0; if(i^chu[dep+1])///找寻成功 flag=dfs(i,dep+1,1); else flag=dfs(i,dep+1,yes); if(flag)return 1; g[u][i]=g[i][u]=1; } return 0;}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { memset(g,0,sizeof(g)); for(int i=0;i<=m;++i) { scanf("%d",&chu[i]);if(i)g[chu[i]][chu[i-1]]=g[chu[i-1]][chu[i]]=1; } if(dfs(chu[0],0,0)) { for(int i=0;i<=m;++i) printf("%d ",ans[i]); puts(""); } else printf("No solution\n"); }}
- CF-62D - Wormhouse(欧拉回路)
- 欧拉回路的一点研究与Codeforces 62D Wormhouse
- CF 528C 欧拉回路构造
- CodeForces 21D Traveling Graph 状压dp+欧拉回路
- 【欧拉回路】 Codeforces 288 D Tanya and Password
- codeforces #547D Mike and Fish 欧拉回路
- 【欧拉路】codeforces #547D Mike and Fish 欧拉回路
- Codeforces Round #369 (Div. 2) D 欧拉回路
- Codeforces D. Wormhouse (dfs+剪枝)
- HDU1878 欧拉回路【欧拉回路】
- 欧拉回路判断
- 欧拉回路 hdu1878
- 欧拉回路
- 欧拉回路
- 欧拉回路
- 欧拉回路,欧拉路
- 欧拉回路
- 欧拉回路
- IaaS PaaS和SaaS公司都做些什么
- fatal error C1083: 无法打开包括文件:“atlapp.h”解决方法
- JS继承的实现
- 人生无根蒂,飘如陌上尘.
- linux设备驱动归纳总结(八):1.总线、设备和驱动
- CF-62D - Wormhouse(欧拉回路)
- sting split
- 非专业levelDB源码解析3:Env
- hdu1116 欧拉路径
- 蓝牙技术概述
- iOS 使用UItableview实现宫格现实-以九宫格为例子
- 人生无根蒂,飘如陌上尘.
- iOS学习笔记19—NSString中 去掉空格
- jQuery .tmpl() 用法