Ultraviolet A 10054 (Euler's loop)

Source: Internet
Author: User

Here we will popularize the knowledge of Euler's loop.

Euler's road refers to any point that begins to traverse, and each side passes through exactly once. Such a road is Euler's road.

If you can start from any point and return to this point, each side goes through exactly once. This is the Euler loop.

Determination of Euler's path: In an undirected graph, there must be an Euler's path, starting from one singularity to another; if all are even vertices, there must be an Euler loop. For a directed graph, the inbound degrees of a maximum of two points are not equal to the outbound degrees. The inbound degrees of a point must be 1 greater than the outbound degrees. This point serves as the end point, the other 1.1 must be 1 greater than the inbound level as the starting point. If there are no points with different outbound and inbound degrees, there is an Euler loop. But the premise must be that the graph is connected.

Next is the question: there are n beads, each of which has two halves, one half of which has a color, and two adjacent beads are required to have the same color as the other half, can it make up a necklace.

Analysis: In the beginning, beads are directly used as nodes, which is difficult to solve. Later, I saw Shen Shu and used the color as the node and the beads as the edge, that is, one color on the beads is connected with another color, but note that, there may be many beads, so pay attention to the processing of duplicate edges! Then, each side is taken once, which is equivalent to the beads used once. Then, starting from any color, the color is finally taken back, which is exactly the Euler loop!

Code:

#include <cstdio>#include <cstring>#include <stack>using namespace std;const int N = 60;int n, T;int g[N][N], edge[N];bool iseu, vis[N][N];struct E{ int u, v; } tmp;stack <E> st;void euler( int u ) {    for ( int v = 1; v <= 50; ++v ) if ( g[u][v] ) {        g[u][v]--; g[v][u] = g[u][v];        euler( v );        tmp.u = u, tmp.v = v;        st.push(tmp);    }}int main() {    scanf("%d", &T);    int icase = 1;    while ( T-- ) {        scanf("%d", &n);        memset( g, 0, sizeof(g) );        memset( vis, 0, sizeof(vis) );        memset( edge, 0, sizeof(edge) );        int s, e;        for ( int i = 0; i < n; ++i ) {            scanf("%d%d", &s, &e);            edge[s]++, edge[e]++;            g[s][e]++;            g[e][s] = g[s][e];        }        iseu = true;        for ( int i = 1; i <= 50; ++i ) if ( edge[i] % 2 == 1 ) iseu = false;        printf("Case #%d\n", icase++ );        if ( !iseu )             printf("some beads may be lost\n");        else {            euler( s );            while ( !st.empty() ) {                tmp = st.top(); st.pop();                printf("%d %d\n", tmp.u, tmp.v);            }        }        if ( T > 0 ) printf("\n");    }}            

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.