zoukankan      html  css  js  c++  java
  • POJ1386Play on Words[有向图欧拉路]

    Play on Words
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 11846   Accepted: 4050

    Description

    Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us. 

    There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word ``acm'' can be followed by the word ``motorola''. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door. 

    Input

    The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters 'a' through 'z' will appear in the word. The same word may appear several times in the list. 

    Output

    Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times. 
    If there exists such an ordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.". 

    Sample Input

    3
    2
    acm
    ibm
    3
    acm
    malform
    mouse
    2
    ok
    ok
    

    Sample Output

    The door cannot be opened.
    Ordering is possible.
    The door cannot be opened.

    Source


    一些知识:

    1、无向图存在欧拉回路条件:每个点度都为偶数

    2、无向图存在欧拉路条件:只有两个点度为奇数或每个点度都为偶数

    3、有向图存在欧拉回路条件:每个点入度都等于出度

    4、有向图存在欧拉路条件:只存在这样两个点:一个点入度等于出度+1,另一个点入度=出度-1,其他每个点入度都等于出度,或者每个点入度都等于出度。

    判连通,判条件就好了

    奇怪的是,dfs就WA,并查集就可以

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    using namespace std;
    typedef long long ll;
    const int N=1e5+5,L=1e3+5;
    int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
        return x*f;
    }
    int T,n,ind[30],outd[30],has[30];
    char s[L];
    //int vis[30],g[30][30];
    //void dfs(int u){//printf("dfs %d
    ",u);
    //    vis[u]=1;
    //    for(int v=0;v<=25;v++)
    //        if(!vis[v]) dfs(v);
    //}
    int fa[30],root=0;
    inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
    int main(){
        T=read();
        while(T--){
            int flag=1;
            for(int i=0;i<=25;i++) ind[i]=outd[i]=has[i]=0,fa[i]=i;
            //memset(g,0,sizeof(g));
            n=read();
            for(int i=1;i<=n;i++){
                scanf("%s",s+1);
                int len=strlen(s+1),u=s[1]-'a',v=s[len]-'a';
                //g[u][v]=g[v][u]=1;
                fa[find(u)]=find(v);root=find(v);
                outd[u]++;ind[v]++;
                has[u]=has[v]=1;
            }
            //for(int i=0;i<=25;i++) if(has[i]) {dfs(i);break;}
            //for(int i=0;i<=25;i++) if(has[i]&&!vis[i]) {flag=0;break;}
            for(int i=0;i<=25;i++) if(has[i]&&find(i)!=root){flag=0;break;}
            if(flag){
                int more=0,less=0;
                for(int i=0;i<=25;i++){
                    if(abs(outd[i]-ind[i])>=2) {flag=0;break;}
                    if(outd[i]==ind[i]+1) more++;
                    if(outd[i]==ind[i]-1) less++;
                }
                if(flag){
                    if((more==0&&less==0)||(more==1&&less==1)) printf("Ordering is possible.
    ");
                    else flag=0;
                }
            }
            if(flag==0) printf("The door cannot be opened.
    ");
        }
    }
  • 相关阅读:
    day 30 粘包 自定义报头
    day29 网络基础之网络协议和通信
    day28 面向对象的进阶 反射 和类的内置方法
    day 27 模块和包 面向对象的复习
    CGI,FastCGI,PHP-CGI和PHP-FPM的区别
    跨平台的移动应用开发引擎CrossApp简介
    element-ui组件中的select等的change事件中传递自定义参数
    关于setInterval和setTImeout中的this指向问题
    懒加载和预加载的区别
    vueX的五个核心属性
  • 原文地址:https://www.cnblogs.com/candy99/p/5907657.html
Copyright © 2011-2022 走看看