zoukankan      html  css  js  c++  java
  • uva10596Morning Walk

    题目就是求一个无向图的欧拉回路,用dfs判断连通性然后判断度是偶数就行了。 特别注意R=0的情况输出not

    题目:

    Problem H

    Morning Walk

    Time Limit

    3 Seconds

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

    Input

    Input will consist of several test cases. Each test case will start with a line containing two numbers. The first number indicates the number of road intersections and is denoted by N (2 ≤ N ≤ 200). The road intersections are assumed to be numbered from 0 to N-1. The second number R denotes the number of roads (0 ≤ R ≤ 10000). Then there will be R lines each containing two numbers c1 and c2 indicating the intersections connecting a road.

    Output

    Print a single line containing the text “Possible” without quotes if it is possible for Kamal to visit all the roads exactly once in a single walk otherwise print “Not Possible”.

    Sample Input

    Output for Sample Input

    2 2

    0 1

    1 0

    2 1

    0 1

    Possible

    Not Possible

    代码:

     1     #include <iostream>
     2     #include <memory.h>
     3     using namespace std;
     4 
     5 
     6     int N,R;
     7     const int maxn = 300;
     8     int G[maxn][maxn];
     9     int du[maxn];
    10     int vis[maxn];
    11 
    12     void dfs(int u)
    13     {
    14         vis[u]=1;
    15         for(int i=0;i<N;i++)
    16         {
    17             if(G[u][i] &&!vis[i])
    18             {
    19                 dfs(i);
    20             }
    21         }
    22     }
    23 
    24 
    25     bool DFS()
    26     {
    27         int cnt=0;
    28         for(int i=0;i<N;i++)
    29         {
    30             if(du[i] && !vis[i])
    31             {
    32                 dfs(i);
    33                 cnt++;
    34             }
    35             if(cnt>1)return false;
    36         }
    37         return true;
    38     }
    39     bool euler()
    40     {
    41         for(int i=0;i<N;i++)
    42         {
    43             if(du[i]%2==1)
    44                 return false;
    45         }
    46         return true;
    47 
    48     }
    49 
    50     int main()
    51     {
    52         while(cin>>N>>R)
    53         {
    54                 int ta,tb;
    55 
    56                 for(int i=0;i<R;i++)
    57                 {
    58                     cin>>ta>>tb;
    59                     G[ta][tb]=G[tb][ta]=1;
    60                     du[ta]++;du[tb]++;
    61                 }
    62                 bool flag = true;
    63 
    64 
    65                 if(R==0){flag=false;cout<<"Not Possible"<<endl;}
    66 
    67                 if(!DFS()){flag=false;cout<<"Not Possible"<<endl;}
    68 
    69 
    70                 if(flag)
    71                 {
    72                     if(!euler())
    73                     {
    74                         cout<<"Not Possible"<<endl;
    75                     }
    76                     else
    77                     {
    78                         cout<<"Possible"<<endl;
    79                     }
    80                 }
    81 
    82                 memset(G,0,sizeof(G));
    83                 memset(vis,0,sizeof(vis));
    84                 memset(du,0,sizeof(du));
    85                 //memset(out,0,sizeof(out));
    86         }
    87 
    88         return 0;
    89     }
  • 相关阅读:
    著名的二分查找的BUG
    C/C++ static用法
    浅谈C++虚函数
    git备忘(长久更新)
    【经典问题】最大子串和
    水波纹效果
    博客迁址 xpeng.scorpionstudio.com
    终于,我们的新产品Fotor Slideshow Maker上线了!!
    分享一款浏览器扩展--美图搜索-图片搜索工具
    分享网页微信防撤回插件
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3446567.html
Copyright © 2011-2022 走看看