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

    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.

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <algorithm>
      4 using namespace std;
      5 int par[26];
      6 int id[26];
      7 int od[26];  ///par为并查集,id为入度,od为出度
      8 int vis[26]; ///vis为该顶点出现过的标记
      9 int finds(int x)///并查集的查找函数
     10 {
     11     int r = x;
     12     while(r != par[r])
     13     {
     14         r = par[r];
     15     }
     16     int i = x, j;
     17     while(i != r)///路径压缩
     18     {
     19         j = par[i];
     20         par[i] = r;
     21         i = j;
     22     }
     23     return r;
     24 }
     25 void join(int x, int y)///并查集的合并函数
     26 {
     27     int fx = finds(x);
     28     int fy = finds(y);
     29     if(fx != fy)///如果x,y在两个不同的连通分量上,那么合并
     30     {
     31         par[fy] = fx;
     32     }
     33 }
     34 int main()
     35 {
     36     int t, n, i;
     37     int u,v;
     38     char str[1005];
     39     scanf("%d", &t);
     40     while(t--)
     41     {
     42         memset(id, 0, sizeof(id));///初始化各个数组
     43         memset(od, 0, sizeof(od));
     44         memset(vis, false, sizeof(vis));
     45         for(i = 0; i <26; i++)///初始化并查集,根节点为自身
     46         {
     47             par[i] = i;
     48         }
     49         scanf("%d", &n);
     50         while(n--)
     51         {
     52             scanf("%s", str);
     53             int len = strlen(str);
     54             u=str[0]-'a';
     55             v=str[len-1]-'a';
     56             join(u,v);///合并出现的顶点
     57             id[v]++;///相应的入度+1
     58             od[u]++;///相应的出度+1
     59             vis[u]=vis[v]=1;///标记该顶点出现过
     60         }
     61         int flag = 0, tag = 0, a = 0, b = 0; ///flag来判图是否连通,tag为图中顶点入度不等于出度的个数
     62         ///a为入度大出度1的点的个数,b为出度大入度1的点的个数
     63         for(i = 0; i <26; i++)///判连通
     64         {
     65             if(vis[i] && par[i] == i)///如果连通,那么所有的点将被划分到一个集合之中
     66             {
     67                 flag++;
     68             }
     69         }
     70         if(flag > 1)///不连通,直接输出
     71         {
     72             printf("The door cannot be opened.
    ");
     73         }
     74         else///flag必须为1
     75         {
     76             for(i = 0; i <26; i++)///查找tag,a, b 的个数
     77             {
     78                 if(vis[i] && id[i] != od[i])
     79                 {
     80                     tag++;
     81                 }
     82                 if(vis[i] && id[i]-od[i] == 1)
     83                 {
     84                     a++;
     85                 }
     86                 if(vis[i] && od[i]-id[i] == 1)
     87                 {
     88                     b++;
     89                 }
     90             }
     91             if(tag == 0)///tag = 0,对于所有的点入度等于初度,存在欧拉回路
     92             {
     93                 printf("Ordering is possible.
    ");
     94             }
     95             else if(a == b && a == 1 && tag == 2)///a = 1 && b = 1 && tag = 2.存在欧拉通路
     96             {
     97                 printf("Ordering is possible.
    ");
     98             }
     99             else
    100             {
    101                 printf("The door cannot be opened.
    ");
    102             }
    103         }
    104     }
    105     return 0;
    106 }


  • 相关阅读:
    C#学习记录二:高级数据存储方式
    SharePoint 2010 匿名用户调用Client Object Model访问列表项
    Android 在闹钟开机时,如何解决开机动画没有播完就进入Launcher M
    Getting in Line UVA 216
    Android 如何关闭Navigation Bar M
    google protocol buffer 简介 版本 安装 使用 实例
    Android [VP]视频播放器播放本地视频时收到短信/彩信,需要界面提示 M
    Maven教程初级篇02:pom.xml配置初步
    当Ruby的model名字出错时,在现实view时显示错误的提示
    VS Code 安装 C++ 调试环境
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9627986.html
Copyright © 2011-2022 走看看