当前位置:网站首页>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
边栏推荐
- Introduction to ins/gps integrated navigation type
- Kubernetes source code analysis (II) -- resource
- Redis入门完整教程:问题定位与优化
- Le tube MOS réalise le circuit de commutation automatique de l'alimentation principale et de l'alimentation auxiliaire, et la chute de tension "zéro", courant statique 20ua
- 换个姿势做运维!GOPS 2022 · 深圳站精彩内容抢先看!
- A complete tutorial for getting started with redis: AOF persistence
- Cryptography series: detailed explanation of online certificate status protocol OCSP
- 房费制——登录优化
- Use of promise in ES6
- Redis introduction complete tutorial: client case analysis
猜你喜欢

Left path cloud recursion + dynamic planning

硬件之OC、OD、推挽解释
![[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]

Household appliance industry under the "retail is king": what is the industry consensus?

Development of wireless communication technology, cv5200 long-distance WiFi module, UAV WiFi image transmission application

input_delay

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

Don't you know the relationship between JSP and servlet?

centerX: 用中国特色社会主义的方式打开centernet

PSINS中19维组合导航模块sinsgps详解(时间同步部分)
随机推荐
Redis Getting started tutoriel complet: positionnement et optimisation des problèmes
Summary of research status of inertial navigation calibration at home and abroad (abridged version)
Utilisation de la promesse dans es6
netperf 而网络性能测量
Lingyun going to sea | yidiantianxia & Huawei cloud: promoting the globalization of Chinese e-commerce enterprise brands
Codeforces round 264 (Div. 2) C gargari and Bishop [violence]
杰理之电话本获取【篇】
How to design interface test cases? Teach you a few tips to draft easily
Nuggets quantification: obtain data through the history method, and use the same proportional compound weight factor as Sina Finance and snowball. Different from flush
[secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
PSINS中19维组合导航模块sinsgps详解(滤波部分)
又一百万量子比特!以色列光量子初创公司完成1500万美元融资
leetcode-02(链表题)
SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
Analysis of USB network card sending and receiving data
Shell 编程基础
MOS transistor realizes the automatic switching circuit of main and auxiliary power supply, with "zero" voltage drop and static current of 20ua
Left value, right value
CVPR 2022 最佳论文候选 | PIP: 6个惯性传感器实现全身动捕和受力估计