HDU 3414 Tour Route (Hamilton Loop Determination)

Source: Internet
Author: User

Test instructions: Given an adjacency matrix to get a graph, to determine if there is Hamiltonian circuit, if present, output path, otherwise output-1;

Idea: Each point as head traverse once to find Hamiltonian path, see if there is Hamiltonian circuit; a point need to be a special sentence;

#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;intn,m;intmm[1005][1005];intnxt[500010];intcnt[500010],ans;voidSolveintStartintnum) {    intj,k; memset (NXT,-1,sizeof(NXT)); intHead=start;  for(intI=1; i<=num;i++){        if(I==start)Continue; if(Mm[i][head]) {Nxt[i]=Head; Head=i; Continue; } J=Head; K=Nxt[j];  while(k!=-1){            if(Mm[j][i]&&mm[i][k]) Break; J=K; K=Nxt[j]; } Nxt[j]=i; Nxt[i]=K; } ans=0;  while(head!=-1) {cnt[++ans]=Head; Head=Nxt[head]; }}intMain () {inti,j,k,flag,u,v;  while(SCANF ("%d", &n)! =EOF) {    if(n==0) Break;  for(i=1; i<=n;i++){       for(j=1; j<=n;j++) {scanf ("%d",&Mm[i][j]); }    }    if(n==1) {printf ("1\n");Continue; } Flag=1;  for(i=1; i<=n;i++) {solve (i,n); if(!mm[cnt[n]][cnt[1]])Continue;  for(j=1; j<=n;j++){            if(j==1) printf ("%d", Cnt[j]); Elseprintf"%d", Cnt[j]); }printf ("\ n"); Flag=0; Break; }    if(flag) printf ("-1\n"); }  return 0;}

HDU 3414 Tour Route (Hamilton Loop Determination)

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.