• POJ 1300 判断欧拉回路


    题意:能否找到一条路径经过所有开着门的房间,并使得:1:通过门后立即把门关上,2:关上的门不在打开,3:最后回到你自己的房间(房间0),并且所有的门都已经关闭。

    题目已知这是连通图。

    分析:以房间为顶点,连接房间之间的门为边构造图。根据题意,输入文件的每个测试数据所构造的图都是连通的。本题实际上是判断一个图中是否存在欧拉回路或者欧拉通路。

    无向图存在欧拉回路的充要条件

    一个无向图存在欧拉回路,当且仅当该图所有顶点度数都是偶数且该图是连通图。

    有向图存在欧拉回路的充要条件

    一个有向图存在欧拉回路,所有顶点的入度等于出度且该图是连通图
    这题是无向图。
     
    这题的关键其实是在读入输入。。。1:因为有空行,而且这些空行都是有用的信息,所以不能简单的用cin||scanf输入每个房间通往其他房间的房间号。
    注意下面代码。
    View Code
    // I'm the Topcoder
    //C
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>
    #include <time.h>
    //C++
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <cctype>
    #include <stack>
    #include <string>
    #include <list>
    #include <queue>
    #include <map>
    #include <vector>
    #include <deque>
    #include <set>
    using namespace std;
    
    //*************************OUTPUT*************************
    #ifdef WIN32
    #define INT64 "%I64d"
    #define UINT64 "%I64u"
    #else
    #define INT64 "%lld"
    #define UINT64 "%llu"
    #endif
    
    //**************************CONSTANT***********************
    #define INF 0x3f3f3f3f
    #define eps 1e-8
    #define PI acos(-1.)
    #define PI2 asin (1.);
    typedef long long LL;
    //typedef __int64 LL;   //codeforces
    typedef unsigned int ui;
    typedef unsigned long long ui64;
    #define MP make_pair
    typedef vector<int> VI;
    typedef pair<int, int> PII;
    #define pb push_back
    #define mp make_pair
    
    //***************************SENTENCE************************
    #define CL(a,b) memset (a, b, sizeof (a))
    #define sqr(a,b) sqrt ((double)(a)*(a) + (double)(b)*(b))
    #define sqr3(a,b,c) sqrt((double)(a)*(a) + (double)(b)*(b) + (double)(c)*(c))
    
    //****************************FUNCTION************************
    template <typename T> double DIS(T va, T vb) { return sqr(va.x - vb.x, va.y - vb.y); }
    template <class T> inline T INTEGER_LEN(T v) { int len = 1; while (v /= 10) ++len; return len; }
    template <typename T> inline T square(T va, T vb) { return va * va + vb * vb; }
    
    // aply for the memory of the stack
    //#pragma comment (linker, "/STACK:1024000000,1024000000")
    //end
    const int maxn = 1010;
    char s[maxn];
    char s2[maxn];
    int n,m;
    int door[maxn];//计算每个房间的门数
    int doors;//门的总数
    int even,odd;
    int main(){
        while(cin>>s){
            if(strcmp(s,"ENDOFINPUT")==0){
                break;
            }
            doors=0; even=0; odd=0;
            getchar();
            scanf("%d%d",&m,&n);
            getchar();//不可少;
            memset(door,0,sizeof(door));
            for(int i=0;i<n;i++){
                gets(s2);
                int len=strlen(s2);
                for(int j=0;j<len;j++){
                    int end=0;
                    while(s2[j]!=' '&&j<len){
                        end=s2[j]-48;
                        j++;
                        door[i]++;//
                        door[end]++;
                        doors++;
                    }
                }
            }
            gets(s);
            for(int i=0;i<n;i++){
                if(door[i]%2==0) even++;
                else odd++;
            }
    
            if(odd==0&&m==0) printf("YES %d\n",doors);
            else if(odd==2&&door[m]%2==1&&door[0]%2==1&&m!=0){
                printf("YES %d\n",doors);
            }
            else printf("NO\n");
        }
        return 0;
    }
  • 相关阅读:
    在jQuery中Ajax的Post提交中文乱码的解决方案
    mysql 日期时间型的按日期分组
    mysql 逗号分隔的id转为逗号分隔的名称
    阿米在日本工作生活趣事(2)
    阿米在日本工作生活趣事(1)
    com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
    File exists.If no other git process is currently running,
    带小数点的String 转int java.lang.Double cannot be cast to java.lang.Integer
    Jboss解决只能通过localhost访问而不能使用IP访问项目的问题
    This method accesses the value of a Map entry, using a key that was retrieved from a keySet iterator. It is more efficient to use an iterator on the entrySet of the map, to avoid the Map.get(key) look
  • 原文地址:https://www.cnblogs.com/lanjiangzhou/p/2988887.html
Copyright © 2020-2023  润新知