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     }
  • 相关阅读:
    多种 网页文本编辑器 分析
    struts2 令牌 实现源代码 JSP
    ibatis 数据库时间 插入数据
    Eclipse/Myeclipse生成serialVersionUID方法
    SSM框架整合遇到的问题
    搭建Elasticsearch5.6.8 分布式集群
    使用SuperWebSocket 构建实时 Web 应用
    HAPROXY
    .NET面试题系列(二)GC
    MongoDB-3.4集群搭建:分片
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3446567.html
Copyright © 2011-2022 走看看