当前位置:网站首页>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
边栏推荐
- 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?
- 简单冒泡排序
- How to analyze fans' interests?
- Kysl Haikang camera 8247 H9 ISAPI test
- MOS transistor realizes the automatic switching circuit of main and auxiliary power supply, with "zero" voltage drop and static current of 20ua
- [2022 national tournament simulation] polygon - computational geometry, binary answer, multiplication
- QT Bluetooth: qbluetooth DeviceInfo
- 又一百万量子比特!以色列光量子初创公司完成1500万美元融资
- New benchmark! Intelligent social governance
- Static proxy of proxy mode
猜你喜欢

How-PIL-to-Tensor

巴比特 | 元宇宙每日必读:IP授权是NFT的破圈之路吗?它的难点在哪里?Holder该如何选择合作平台?...

wireshark安装

Google Earth engine (GEE) -- 1975 dataset of Landsat global land survey

Five reasons for clothing enterprises to deploy MES management system

OC, OD, push-pull explanation of hardware

PSINS中19维组合导航模块sinsgps详解(时间同步部分)
Django database (SQLite) basic introductory tutorial

Oauth2协议中如何对accessToken进行校验

How to verify accesstoken in oauth2 protocol
随机推荐
wireshark安装
Have fun | latest progress of "spacecraft program" activities
Code debugging core step memory
Oracle中日期的使用方法实例
哈希表及完整注释
Five reasons for clothing enterprises to deploy MES management system
Redis入门完整教程:RDB持久化
uniapp适配问题
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?
基于ensp防火墙双击热备二层网络规划与设计
C language string sorting
Redis introduction complete tutorial: client case analysis
Construction of knowledge map of mall commodities
Use of tensorboard
Redis入门完整教程:复制拓扑
从控制理论的角度谈数据分析
Left value, right value
Django database (SQLite) basic introductory tutorial
Es6中Promise的使用
Data analysis from the perspective of control theory