当前位置:网站首页>HDU 4337 King Arthur's Knights 它输出一个哈密顿电路
HDU 4337 King Arthur's Knights 它输出一个哈密顿电路
2022-07-06 19:36:00 【全栈程序员站长】
大家好,又见面了,我是全栈君
n积分m文章无向边
它输出一个哈密顿电路
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 155;
int n, m;
bool mp[N][N];
int S, T, top, Stack[N];
bool vis[N];
void _reverse(int l,int r) {
while (l<r)
swap(Stack[l++],Stack[r--]);
}
void expand() {
while(1) {
bool flag = 0;
for (int i=1; i<=n && false == flag; i++)
if (!vis[i] && mp[T][i])
{
Stack[top++]=i;
T=i;
flag = vis[i] = 1;
}
if (!flag) return;
}
}
void hamiltun(int Start){
memset(vis, 0, sizeof vis);
S = Start;
for(T=2; T<=n; T++) //随意找两个相邻的节点S和T
if (mp[S][T]) break;
top = 0;
Stack[top++]=S;
Stack[top++]=T;
vis[S] = vis[T] = true;
while (1)
{
expand(); //在它们基础上扩展出一条尽量长的没有反复节点的路径:步骤1
_reverse(0,top-1);
swap(S,T);
expand(); //在它们基础上扩展出一条尽量长的没有反复节点的路径
int mid=0;
if (!mp[S][T]) //若S与T不相邻,能够构造出一个回路使新的S和T相邻
{
//设路径S→T上有k+2个节点,依次为S,v1,v2…… vk和T.
//能够证明存在节点vi,i∈[1,k),满足vi与T相邻,且vi+1与S相邻
for (int i=1; i<top-2; i++)
if (mp[Stack[i]][T] && mp[Stack[i+1]][S])
{
mid=i+1; break;
}
//把原路径变成S→vi→T→vi+1→S,即形成了一个回路
_reverse(mid,top-1);
T=Stack[top-1];
}
if (top==n) break;
//如今我们有了一个没有反复节点的回路.假设它的长度为N,则汉密尔顿回路就找到了
//否则,因为整个图是连通的,所以在该回路上,一定存在一点与回路以外的点相邻
//那么从该点处把回路断开,就变回了一条路径,再依照步骤1的方法尽量扩展路径
for (int i = 1, j; i <= n; i++)
if (!vis[i])
{
for (j=1; j<top-1; j++)
if (mp[Stack[j]][i]) break;
if (mp[Stack[j]][i])
{
T=i; mid=j;
break;
}
}
S=Stack[mid-1];
_reverse(0,mid-1);
_reverse(mid,top-1);
Stack[top++]=T;
vis[T]=true;
}
}
int main() {
while (cin>>n>>m) {
memset(mp, 0, sizeof mp);
for (int i = 1, u, v; i <= m; i++) {
scanf("%d %d",&u, &v);
mp[u][v] = mp[v][u] = 1;
}
hamiltun(1);
for (int i = 0; i < top; i++)
printf("%d%c", Stack[i], i==top-1?'\n':' ');
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116761.html原文链接:https://javaforall.cn
边栏推荐
- Read fast RCNN in one article
- Cloud Mail . NET Edition
- [2022 national tournament simulation] polygon - computational geometry, binary answer, multiplication
- Wireshark installation
- The whole process of knowledge map construction
- Huitong programming introductory course - 2A breakthrough
- Error in fasterxml tostringserializerbase
- Redis Getting started tutoriel complet: positionnement et optimisation des problèmes
- Redis入门完整教程:复制配置
- 杰理之播内置 flash 提示音控制播放暂停【篇】
猜你喜欢
IDEA重启后无法创建Servlet文件的解决方案
Redis入门完整教程:RDB持久化
A complete tutorial for getting started with redis: problem location and optimization
Statistics of radar data in nuscenes data set
Cryptography series: detailed explanation of online certificate status protocol OCSP
MOS transistor realizes the automatic switching circuit of main and auxiliary power supply, with "zero" voltage drop and static current of 20ua
尚硅谷JVM-第一章 类加载子系统
The 8 element positioning methods of selenium that you have to know are simple and practical
Django数据库(SQlite)基本入门使用教程
Redis Getting started tutoriel complet: positionnement et optimisation des problèmes
随机推荐
Cryptography series: detailed explanation of online certificate status protocol OCSP
Unity uses maskablegraphic to draw a line with an arrow
左程云 递归+动态规划
QT common Concepts-1
New benchmark! Intelligent social governance
Unity webgl adaptive web page size
Django数据库(SQlite)基本入门使用教程
Derivative, partial derivative, directional derivative
Left value, right value
Redis入门完整教程:问题定位与优化
A complete tutorial for getting started with redis: problem location and optimization
PSINS中19维组合导航模块sinsgps详解(时间同步部分)
巴比特 | 元宇宙每日必读:IP授权是NFT的破圈之路吗?它的难点在哪里?Holder该如何选择合作平台?...
Static proxy of proxy mode
Redis入门完整教程:AOF持久化
Redis入门完整教程:复制配置
c语言(字符串)如何把字符串中某个指定的字符删除?
How to find file accessed / created just feed minutes ago
Django database (SQLite) basic introductory tutorial
如何分析粉丝兴趣?