zoukankan      html  css  js  c++  java
  • (判断欧拉回路)poj 1368

    Play on Words
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 10056   Accepted: 3447

    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.
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    #include<string>
    #include<vector>
    using namespace std;
    vector<int> e[27];
    int tt,n,in[27],out[27],vis[27];
    char s[100010];
    void dfs(int x)
    {
          vis[x]=1;
          for(int i=0;i<e[x].size();i++)
                if(vis[e[x][i]]==0)
                      dfs(e[x][i]);
    }
    bool ok(int x)
    {
          dfs(x);
          for(int i=0;i<26;i++)
                if(vis[i]==0&&e[i].size()!=0)
                      return false;
          return true;
    }
    int main()
    {
          scanf("%d",&tt);
          while(tt--)
          {
                memset(in,0,sizeof(in));
                memset(out,0,sizeof(out));
                memset(vis,0,sizeof(vis));
                int len,a,b;
                scanf("%d",&n);
                for(int i=0;i<27;i++)
                      e[i].clear();
                for(int i=0;i<n;i++)
                {
                      scanf("%s",s);
                      len=strlen(s);
                      a=s[0]-'a',b=s[len-1]-'a';
                      out[a]++,in[b]++;
                      e[a].push_back(b);
                      e[b].push_back(a);
                }
                if(!ok(a))
                {
                      printf("The door cannot be opened.
    ");
                      continue;
                }
                bool flag=false;
                int j;
                for(j=0;j<26;j++)
                {
                      if(in[j]!=out[j])
                      {
                            if(in[j]<out[j])
                            {
                                  if(in[j]-out[j]==-1&&flag==0)
                                  {
                                        flag=1;
                                  }
                                  else
                                  {
                                        break;
                                  }
                            }
                      }
                }
                if(j<26)
                      printf("The door cannot be opened.
    ");
                else
                      printf("Ordering is possible.
    ");
          }
          return 0;
    }
    

      注意连通性

  • 相关阅读:
    RESTful API 架构解读
    在阿里云 ECS 搭建 nginx https nodejs 环境(三、nodejs)
    在阿里云 ECS 搭建 nginx https nodejs 环境(二、https)
    linux 常见操作指令
    前端数据存储方案集合(cookie localStorage等)以及详解 (二)
    无法从其“Checked”属性的字符串表示形式“checked”创建“System.Boolean”类型
    VS2012使用验证控件出现[ASP.NET]WebForms UnobtrusiveValidationMode 需要 'jquery' 的 ScriptResourceMapping。請加入 ScriptResourceMapping 命名的 jquery (區分大小寫)。的解决办法。
    使用SHFB(Sandcastle Help File Builder)建立MSDN风格的代码文档
    常用正则表达式总结
    sql 中有关时间的语句
  • 原文地址:https://www.cnblogs.com/a972290869/p/4245524.html
Copyright © 2011-2022 走看看