HDU 4337--poj 2438 (Hamilton Loop Solver template)

Source: Internet
Author: User
Tags extend

Ext: http://imlazy.ycool.com/post.2072698.html

Dirac theorem: There are N nodes in a acyclic graph , and if all nodes have degrees greater than or equal to N/2, then the Hamilton loop must exist. Note that division in "N/2" is not divisible, but real division. If n is an even number, of course there is no ambiguity; if n is odd, then "N/2" in this condition is equivalent to "⌈n/2⌉".
It's not hard to prove. First you can prove that the diagram must be connected. Set D (v) to indicate the degree of Node v. For any two nodes U, V, if they are not adjacent, it may and their adjacent nodes have N-2, and D (U) + d (v) ≥N/2 + n/2≥n, then according to the pigeon Nest principle, there must be a node and both U and V are adjacent. The proof is that any two nodes are connected to each other.

Next, the process of proving the existence of the Hamilton Loop is actually the process of constructing the loop, which is divided into the following steps:

1. Find any two adjacent nodes S and T, and expand them on a path that is as long as possible without repeating nodes. That is, if S is adjacent to Node V, and V is not on the path s→t, then the path can be turned into v→s→t, then v becomes the new S. Extend from S and T to both ends until it cannot be expanded, that is, all nodes adjacent to s or T are on the path s→t.
2. If S and T are adjacent, the path s→t form a loop.
3. If S is not adjacent to T, a circuit can be constructed. The path s→t has k + 2 nodes, followed by S, V1, v2 ... vk and T. It can be proved that there are nodes VI, I∈[1, K, which meet the VI and T adjacent, and the vi+1 is adjacent to S. The proof method is also based on the pigeon Nest principle, since the points adjacent to S and T are all on the path, and the range of distributions is only v1∼vk K dots, k≤n-2, and D (s) + D (T) ≥n, it can be imagined that there must be a point vi adjacent to S and a with T The point VJ, satisfying J < I. Then the above proposition is clearly set up.

After finding the node VI satisfying the condition, we can turn the original path into S→vi+1→t→vi→s, which forms a circuit.
4. Now we have a loop with no duplicate nodes. If its length is N, then the Hamilton Loop is found.

If the length of the loop is less than N, because the whole diagram is connected, there must be a point adjacent to the loop outside the back path. Then disconnect the loop from that point and change back to a path. Then follow step 1 to extend the path as much as possible, and there must be a new node added. Then go back to step 2.

In the entire construction process, if each to step 4 is a round, then because each round, at least one node is added to the path s→t, so the total number of rounds certainly no more than N round. In fact, it's not hard to see the complexity of the algorithm is O (N2), because the total expansion of the N-step path, each extension of the maximum enumeration of all nodes.

Source code problem:2438 user:1013101127 memory:848k time:141ms language:c++ Source code #i
Nclude <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace std;
const int N = 410;
int mp[n][n];
int ans[n];
int vis[n];
int start;
int end;
int n,m;
int cnt;
      void Init () {for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (i==j) mp[i][j]=0;
    else mp[i][j]=1;
    memset (Vis) (vis,0,sizeof);
    memset (ANS) (ans,0,sizeof);
cnt=0;
     } void Reverse (int s,int e) {while (s<e) {swap (ans[s],ans[e));
     s++;
    e--;
        } void Kuozhan () {while (1) {int flag=0; 
                for (int i=1;i<=n;i++) {if (!vis[i]&&mp[end][i]) {ans[cnt++]=i;
                End=i;
                Vis[i]=1;
                flag=1;
            Break
    } if (!flag) break; } void HamilTun () {start=1;
       for (int i=1;i<=n;i++) if (Mp[1][i]) {end=i;break;
    } vis[start]=1;
    Vis[end]=1;
    Ans[0]=start;
    Ans[1]=end;
    cnt=2;
      while (1) {Kuozhan ();
      Reverse (0,cnt-1);
      Swap (start,end);
      Kuozhan ();
      int mid=0; if (!mp[start][end]) {for (int i=1;i<cnt-2;i++) {if (mp[ans[i]][end]&&mp[a
                Ns[i+1]][start]) {mid=i+1;
            Break
          } Reverse (mid,cnt-1);
      END=ANS[CNT-1];
      } if (cnt==n) break;
            for (int i=1;i<=n;i++) {if (!vis[i]) {int J;
            for (j=1;j<cnt-1;j++) if (Mp[ans[j]][i]) {Mid=j;break}
            if (Mp[ans[mid]][i]) {end=i;mid=j;break;
      }} Start=ans[mid-1];
      Reverse (0,mid-1);
      Reverse (mid,cnt-1); ans[cnt++]=end;
    Vis[end]=1;
        int main () {while (scanf ("%d%d", &n,&m)!=eof) {if (!n&&!m) break;
        n*=2;
        int u,v;
        Init ();
          for (int i=1;i<=m;i++) {scanf ("%d%d", &u,&v);
        mp[u][v]=mp[v][u]=0;
        } Hamiltun ();
        cout<<ans[0];
        for (int i=1;i<cnt;i++) printf ("%d", ans[i]);
    cout<<endl;
return 0;
 }



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.