当前位置:网站首页>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
边栏推荐
- Data analysis from the perspective of control theory
- Static proxy of proxy mode
- How-PIL-to-Tensor
- NuScenes数据集关于Radar数据的统计
- Form validation of uniapp
- Redis introduction complete tutorial: replication principle
- OC, OD, push-pull explanation of hardware
- Cloud Mail . NET Edition
- Django database (SQLite) basic introductory tutorial
- Kubernetes源码分析(二)----资源Resource
猜你喜欢
The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
【Socket】①Socket技术概述
How-PIL-to-Tensor
掘金量化:通过history方法获取数据,和新浪财经,雪球同用等比复权因子。不同于同花顺
Huitong programming introductory course - 2A breakthrough
Left value, right value
C language exercises_ one
Analysis of USB network card sending and receiving data
Redis introduction complete tutorial: client case analysis
基于ensp防火墙双击热备二层网络规划与设计
随机推荐
哈希表及完整注释
Andrews - multimedia programming
How-PIL-to-Tensor
What management points should be paid attention to when implementing MES management system
widerperson数据集转化为YOLO格式
Metaforce force meta universe fossage 2.0 smart contract system development (source code deployment)
Redis入門完整教程:問題定比特與優化
uniapp的表单验证
How-PIL-to-Tensor
杰理之FM 模式单声道或立体声选择设置【篇】
Cryptography series: detailed explanation of online certificate status protocol OCSP
Nuggets quantification: obtain data through the history method, and use the same proportional compound weight factor as Sina Finance and snowball. Different from flush
Unity uses maskablegraphic to draw a line with an arrow
C language exercises_ one
What are the characteristics of the operation and maintenance management system
c语言字符串排序
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (initial assignment part)
Uniapp adaptation problem
密码学系列之:在线证书状态协议OCSP详解
Five reasons for clothing enterprises to deploy MES management system