Poj 1041 John's trip

Source: Internet
Author: User

Euler Loop

A directed graph is used to determine whether it is an Euler loop and the output path. The minimum Lexicographic Order is required. In the output, x y z, x, and y are vertex numbers (up to 44 points) and z are edge numbers (numbered here, the maximum number of edges). The output path is not the output vertex but the number of the output edge. Therefore, the minimum Lexicographic Order is the minimum Lexicographic Order of the edge. Each group of data ends at 0. The first row of each array, with two dots x and y, selects the smaller one as the starting point.

In this case, the graph is connected, so you do not need to judge the connectivity. Therefore, to judge whether it is a euclidean, you only need to check whether the degree of each vertex is an even number. If not, there is no Euler loop, if yes, there is an Euler loop.

The path with the smallest Lexicographic Order is output. You can use a graph creation method.

E [k] [0], e [k] [1] indicates the two vertices of the k edge.

We use recursion to output the path, starting from the start point, then enumeration edge, starting from the smallest side of the number, so as to ensure the minimum Lexicographic Order

 

#include <iostream>#include <cstdio>#include <cstring>#include <stack>using namespace std;#define N 50#define M 2010int node,edge,sp,Max;int e[M][2],de[M];bool used[M];stack<int>sta;void dfs(int u){    for(int k=1; k<=edge; k++)        if(!used[k])        {            int v;            if(e[k][0] == u) v = e[k][1];            else if(e[k][1] == u) v = e[k][0];            else continue;            used[k] = true;            dfs(v);            sta.push(k);        }}void solve(){    while(!sta.empty()) sta.pop();    memset(used,false,sizeof(used));    dfs(sp);    bool first = true;    while(!sta.empty())    {        if(!first) cout << " ";        cout << sta.top();        first = false;        sta.pop();    }    cout << endl;}int main(){    int u,v,m;    while(cin >> u >> v)    {        if(u == 0 && v == 0) break;        memset(de,0,sizeof(de));        sp = min(u,v); Max = max(u,v);        cin >> m;        e[m][0] = u;  e[m][1] = v;        de[u]++;  de[v]++;        edge = 1;        while(cin >> u >> v)        {            if(u == 0 && v == 0) break;            Max = max(Max,u); Max = max(Max,v);            cin >> m;            e[m][0] = u; e[m][1] = v;            de[u]++; de[v]++;            edge++;        }        int ok = 1;        for(int i=1; i<=Max; i++)             if(de[i] & 1)            { ok = 0; break;}        if(!ok)        {            cout << "Round trip does not exist." << endl;            continue;        }        solve();    }    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.