UVa 10596:morning Walk, Stark Euler loops

Source: Internet
Author: User
Tags printf stdin

Topic Link:

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_ problem&problem=1537

Type of topic: Euler loop, and check set, DFS

Topic:

Kamal is a motashota guy. He has got a new job in Chittagong. So, he has moved to Chittagong from Dinajpur. He is getting fatter in Dinajpur as him had no work in his hand there. So, the moving to Chittagong has is turned to is a blessing for him. Every morning he takes a walk through the hilly roads to charming city Chittagong. It is enjoying the very much. There are so many roads at Chittagongand every morning he takes different to his paths. But while choosing a path him makes sure he does not visit a road twice not even in his way back home. An intersection point of a road isn't considered as the part of the road. In a sunny morning, he is thinking about how it would being if he could visit all the roads of the "city in a" single walk. Your task is to help Kamal in determining whether it are possible for him.

The main effect of the topic:

Kamal every morning from home to chittagongand this place. From home to Chittagongand there are many roads, he had to choose a route every morning, this route from home to Chittagongand, walking home from Chittagongand. This route cannot be repeated across the same road. There may be several roads between two locations. For example, the route from A to B appears multiple times, which means that every time there is a different path.

Analysis and Summary:

The problem is the naked Euler loop. The UVA 10054-the necklace and UVA 10129-play on Words are described in more detail. No more baggage.

1. Euler Circuit +dfs

#include <cstring> #include <iostream> #include <cstdio> using namespace std;  
     
int Vis[210],n, M, g[210][210], indegree[210], outdegree[210];    
    void Dfs (int v)//depth first traversal {vis[v]=true;    
        for (int i=0;i<n;i++) {if (!vis[i] && g[v][i]) {DFS (i);  
int main () {#ifdef local freopen ("Input.txt", "R", stdin);  
        #endif while (~scanf ("%d%d", &n, &m)) {memset (g, 0, sizeof (g));  
        int a,b;  
        memset (Vis, 0, sizeof (VIS));  
            for (int i=0; i<m; ++i) {scanf ("%d%d", &a,&b);  
            G[A][B] = G[b][a] = 1;   
            ++vis[a];  
        ++VIS[B];  
        int cnt = 0;  
                for (int i=0; i<n; ++i) {if (vis[i]%2==1) {++cnt;  
            Break } memset (Vis, 0, SizeoF (Vis)); if (CNT | |  
        M&LT;2) printf ("Not possible\n");  
            else{DFS (0);  
            BOOL flag = TRUE;  
            for (int i=0; i<n; ++i) {if (!vis[i)) flag = false;  
            } if (flag) printf ("possible\n");  
        else printf ("Not possible\n");  
} return 0; }

2, Euler loop + and search set

#include <cstring> #include <iostream> #include <cstdio> using namespace std;  
     
int Vis[210],n, M, g[210][210], indegree[210], outdegree[210], f[210];    
    void init ()//and look-up set to determine whether it is a connected graph {int i;    
for (i=0;i<n;i++) f[i]=i;    
    int find (int x) {int r=x;    
    while (F[R]!=R) r=f[r];    
    F[x]=r;    
return R;    
    } void Union (int x,int y) {int fx,fy;    
    Fx=find (x);    
    Fy=find (y);    
if (fx!=fy) f[fx]=fy;  
int main () {#ifdef local freopen ("Input.txt", "R", stdin);  
        #endif while (~scanf ("%d%d", &n, &m) && N) {memset (g, 0, sizeof (g));  
        int a,b;  
        Init ();  
        memset (Vis, 0, sizeof (VIS));  
            for (int i=0; i<m; ++i) {scanf ("%d%d", &a,&b);  
            ++G[A][B];  
            ++g[b][a];  
            ++vis[a];  
            ++VIS[B]; Union (A,B);  
        int ans=0;  
             
        for (int i=0; i<n; ++i) if (f[i]==i) ++ans;  
            if (ans==1 && m>=2) {int cnt = 0;  
                    for (int i=0; i<n; ++i) {if (vis[i]%2==1) {++cnt;  
                Break  
            } if (CNT) printf ("Not possible\n");  
        else printf ("possible\n");   
    else printf ("Not possible\n");  
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.