zoukankan      html  css  js  c++  java
  • POJ1386Play on Words(欧拉回路)

                                                             Play on Words
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 11748   Accepted: 4018

    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.
    【题意】这个就相当于成语接龙,需满足前一个单词的尾字母与后一个的首字母相同。然后就是问是否 存在欧拉通路。
    【分析】用两个数组存每个字母的入度与出度,再用vis[]判断字母是否出现,然后并查集判断是否联通。
    定理1:无向图G存在欧拉通路的条件是:G为连通图,并且G只有两个奇度节点或者无奇度节点。
    推论1:(1)当G是仅有两个奇度节点的连通图时,G的欧拉通路必以此两个节点为端点。
    (2)当G是无奇度节点连通图时,G比为欧拉回路。
    (3)G为欧拉图(存在欧拉回路)的充要条件是G为无奇度节点的连通图。
    定理2:有向图D存在欧拉通路的充要条件是:D为有向图,D的基图联通,并且所有顶点的出度与入度都相等;或者除两个顶点外
    其余顶点的出度与入度都相等,而这两个顶点中一个顶点的出度-入度==1,另一个出度-入度==-1;
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #include<functional>
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define pi acos(-1.0)
    using namespace std;
    typedef long long ll;
    const int N=100005;
    const int M=150005;
    ll power(ll a,int b,ll c){ll ans=1;while(b){if(b%2==1){ans=(ans*a)%c;b--;}b/=2;a=a*a%c;}return ans;}
    char str[N];
    int n,m;
    int vis[27],pre[27];
    int out[27],in[27];
    struct man
    {
        int u,v;
    }edg[N];
    void init()
    {
        memset(vis,0,sizeof(vis));
        memset(out,0,sizeof(out));
        memset(in,0,sizeof(in));
        for(int i=0;i<26;i++)pre[i]=i;
    }
    int Find(int x) {
        if(pre[x] != x) pre[x] = Find(pre[x]);
        return pre[x];
    }
    
    void Union(int x,int y) {
        x = Find(x);
        y = Find(y);
        if(x == y) return;
        pre[y] = x;
    }
    bool beconnect()
    {
        for(int i=0;i<n;i++){
            int u=edg[i].u,v=edg[i].v;
            if(u!=v&&Find(u)!=Find(v))Union(u,v);
        }
        int f=-1,k;
        for(k=0;k<26;k++){
            if(!vis[k])continue;
            if(f==-1)f=k;
            else if(Find(k)!=Find(f))break;
        }
        if(k<26)return false;
        return true;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--){
            init();
            scanf("%d",&n);
            for(int i=0;i<n;i++){
                scanf("%s",str);
                int u=str[0]-'a',v=str[strlen(str)-1]-'a';
                edg[i].u=u;edg[i].v=v;
                vis[u]=vis[v]=1;
                out[u]++;in[v]++;
            }
            bool flag=true;
            int cnt1=0,cnt2=0;
            for(int i=0;i<26;i++){
                if(!vis[i])continue;
                if(abs(out[i]-in[i])>1){
                    flag=false;
                    break;
                }
                if(out[i]-in[i]==1){
                    cnt1++;
                    if(cnt1>1){
                        flag=false;
                        break;
                    }
                }
                if(out[i]-in[i]==-1){
                    cnt2++;
                    if(cnt2>1){
                        flag=false;
                        break;
                    }
                }
            }
            if(cnt1!=cnt2)flag=false;
            if(!beconnect())flag=false;
            if(flag)puts("Ordering is possible.");
            else puts("The door cannot be opened.");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    D 最熟悉的陌生人 (纪念当年就读的梅州市江南高级中学)
    C 最熟悉的陌生人 (纪念当年就读的梅州市江南高级中学)
    B 最熟悉的陌生人 (纪念当年就读的梅州市江南高级中学)
    A 最熟悉的陌生人 (纪念当年就读的梅州市江南高级中学)
    奥秘月球背面
    嫦娥二号月球图片
    外星人就在月球背面
    月球背面
    分享一个绿色版本 sql server 查询器,
    java 8 原版 api 下载地址,
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/5771871.html
Copyright © 2011-2022 走看看