当前位置:网站首页>HDU 4337 King Arthur' S Knights it outputs a Hamiltonian circuit
HDU 4337 King Arthur' S Knights it outputs a Hamiltonian circuit
2022-07-07 03:11:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack
n integral m The article has no direction
It outputs a Hamiltonian circuit
#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++) // Randomly find two adjacent nodes S and T
if (mp[S][T]) break;
top = 0;
Stack[top++]=S;
Stack[top++]=T;
vis[S] = vis[T] = true;
while (1)
{
expand(); // On the basis of them, expand a path without repeated nodes as long as possible : step 1
_reverse(0,top-1);
swap(S,T);
expand(); // On the basis of them, expand a path without repeated nodes as long as possible
int mid=0;
if (!mp[S][T]) // if S And T Not adjacent , Can construct a circuit to make a new S and T adjacent
{
// Set path S→T There are k+2 Nodes , In turn S,v1,v2…… vk and T.
// It can prove the existence of nodes vi,i∈[1,k), Satisfy vi And T adjacent , And vi+1 And S adjacent
for (int i=1; i<top-2; i++)
if (mp[Stack[i]][T] && mp[Stack[i+1]][S])
{
mid=i+1; break;
}
// Turn the original path into S→vi→T→vi+1→S, It forms a loop
_reverse(mid,top-1);
T=Stack[top-1];
}
if (top==n) break;
// Now we have a loop without repeating nodes . Suppose its length is N, Then Hamilton loop is found
// otherwise , Because the whole graph is connected , So in this loop , There must be a point adjacent to a point outside the loop
// Then disconnect the circuit from that point , It's a path back , Then follow the steps 1 Try to expand the path
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;
}Copyright notice : This article is the original article of the blogger , Blog , Do not reprint without permission .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116761.html Link to the original text :https://javaforall.cn
边栏推荐
- 尚硅谷JVM-第一章 类加载子系统
- Redis Getting started tutoriel complet: positionnement et optimisation des problèmes
- LeetCode 77:组合
- Cocos2d-x box2d physical engine compilation settings
- 又一百万量子比特!以色列光量子初创公司完成1500万美元融资
- 巴比特 | 元宇宙每日必读:IP授权是NFT的破圈之路吗?它的难点在哪里?Holder该如何选择合作平台?...
- 上个厕所的功夫,就把定时任务的三种调度策略说得明明白白
- Kubernetes源码分析(二)----资源Resource
- Andrews - multimedia programming
- 硬件之OC、OD、推挽解释
猜你喜欢

Redis入门完整教程:客户端案例分析

uniapp适配问题

美国空军研究实验室《探索深度学习系统的脆弱性和稳健性》2022年最新85页技术报告

杰理之在非蓝牙模式下,手机连接蓝牙不要跳回蓝牙模式处理方法【篇】

Es6中Promise的使用

mos管实现主副电源自动切换电路,并且“零”压降,静态电流20uA

首届“量子计算+金融科技应用”研讨会在京成功举办

密码学系列之:在线证书状态协议OCSP详解

Babbitt | metauniverse daily must read: is IP authorization the way to break the circle of NFT? What are the difficulties? How should holder choose the cooperation platform
![[secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]](/img/f0/79e7915ba3ef32aa21c4a1d5f486bd.jpg)
[secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]
随机推荐
Form validation of uniapp
Oauth2协议中如何对accessToken进行校验
杰理之RTC 时钟开发【篇】
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?
Household appliance industry under the "retail is king": what is the industry consensus?
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (initial assignment part)
又一百万量子比特!以色列光量子初创公司完成1500万美元融资
Cryptography series: detailed explanation of online certificate status protocol OCSP
Redis入门完整教程:复制拓扑
Matlab Error (Matrix dimensions must agree)
Redis getting started complete tutorial: common exceptions on the client
尚硅谷JVM-第一章 类加载子系统
OC, OD, push-pull explanation of hardware
LeetCode 77:组合
Es6中Promise的使用
c语言字符串排序
硬件之OC、OD、推挽解释
2022 spring recruitment begins, and a collection of 10000 word interview questions will help you
【Swift】学习笔记(一)——熟知 基础数据类型,编码风格,元组,主张
netperf 而网络性能测量