HDU 4337 King arthur& #39; s Knights it outputs a Hamiltonian circuit

Source: Internet
Author: User

N Points m article no forward side

It outputs a Hamiltonian circuit



#include <cstdio> #include <cstring> #include <iostream>using namespace std;const int N = 155;int N, m;bo Ol 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 TIF (Mp[s][t]) break; top = 0; Stack[top++]=s; Stack[top++]=t;vis[s] = vis[t] = True;while (1) {expand ();//expand on them a path that is as long as possible without repeated nodes: Step 1_reverse (0,top-1); swap (s,t); expand (); On the basis of the extension of a long, no-repeat-node path int mid=0;if (!mp[s][t])//If s is not adjacent to T, can construct a loop so that the new S and T adjacent {//Set the path s→t k+2 nodes, s,v1,v2 ... vk and T. Can prove the existence of node vi,i∈[1,k), satisfies the 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, that is to form a loop _reverse (mid,top-1); T=stack[tOP-1];} if (top==n) break;//Now we have a loop that does not have a repeating node. Assuming it's length is N, then the Hamilton Loop is found//otherwise, because the entire graph is connected, so in this way, there must be a point adjacent to the loop outside the circuit//then from the point where the circuit is disconnected, Change back to a path, and then follow the method of step 1 as far as possible to extend 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]) Brea K;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", & Amp;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 Bo Master original articles, blogs, without consent may not be reproduced.

HDU 4337 King arthur& #39; s Knights it outputs a Hamiltonian circuit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.