• HDOJ1116 Play on Words[并查集+欧拉回路]


    Play on Words

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2938    Accepted Submission(s): 935


    Problem 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
     
    Recommend
    Eddy
     
     
     
     
     
     
     
    题意:将n个单词首尾相连组成一个单词链,如:acm->malform->mouse(a -> m -> m -> m ->m -> e)
       并查集+欧拉路实现
          1.并查集判连通,这点不用多说
          2.欧拉路,由图中可知除二端点外,其余字母的入度和出度均相等,二端点的出度和入度相差1,还有一种可能是,整个图就是一个欧拉回路,此时每个端点的入度和出度均相等
     
     
     
     
     
     
    code:
      1 /*
      2 欧拉回路:不重复地经过每条边的回路。
      3 欧拉路径:不重复地经过每条边的路径。
      4 欧拉图存:在欧拉回路的图。
      5 半欧拉图:存在欧拉路径的图。
      6 
      7 
      8 无向欧拉图的判定
      9 无向图存在欧拉回路的充要条件:连通且没有奇点。
     10 无向图存在欧拉路径的充要条件:连通且奇点个数为2。
     11 
     12 
     13 有向欧拉图的判定
     14 有向图存在欧拉回路的充要条件:基图连通且所有顶点入度等于出度。
     15 有向图存在欧拉路径的充要条件:基图连通且存在某顶点入度比出度大1,另一顶点出度比入度大1,其余顶点入度等于出度。
     16 
     17 
     18 连通性用并查集来判断
     19 */
     20 #include<iostream>
     21 using namespace std;
     22 
     23 #define MAXN 27
     24 
     25 int father[MAXN];
     26 int in[MAXN],out[MAXN];   //记录入度和出度
     27 int flag[MAXN];          //判断是否存在
     28 int data[MAXN];           //用来存储
     29 int n;
     30 
     31 void init()
     32 {
     33     for(int i=0;i<26;i++)
     34         father[i]=i;
     35     memset(in,0,sizeof(in));
     36     memset(out,0,sizeof(out));
     37     memset(flag,0,sizeof(flag));
     38 }
     39 
     40 int findset(int v)
     41 {
     42     while(v!=father[v])
     43         v=father[v];
     44     return v;
     45 }
     46 
     47 void Union(int a,int b)
     48 {
     49     int x,y;
     50     x=findset(a);
     51     y=findset(b);
     52     if(x!=y)
     53         father[x]=y;
     54 }
     55 
     56 int main()
     57 {
     58     int t;
     59     scanf("%d",&t);
     60     while(t--)
     61     {
     62         init();
     63         scanf("%d",&n);
     64         for(int i=0;i<n;i++)
     65         {
     66             char a,b;
     67             char str[1001];
     68             scanf("%s",str);
     69             a=str[0]-'a';
     70             b=str[strlen(str)-1]-'a';
     71             Union(a,b);
     72             in[b]++;
     73             out[a]++;
     74             flag[a]=flag[b]=1;
     75         }
     76         int cnt=0;
     77         for(int i=0;i<26;i++)
     78         {
     79             father[i]=findset(i);
     80             if(father[i]==i&&flag[i])
     81                 cnt++;                  //计算连通分支个数
     82         }
     83         if(cnt>1)        //说明图不连通
     84         {
     85             printf("The door cannot be opened.\n");
     86             continue;
     87         }
     88         int k=0;
     89         for(int i=0;i<26;i++)
     90         {
     91             if(flag[i]&&in[i]!=out[i])
     92                 data[k++]=i;
     93         }
     94         if(k==0)
     95         {
     96             printf("Ordering is possible.\n");
     97             continue;
     98         }
     99         int temp1=data[0];
    100         int temp2=data[1];
    101         if(k==2&&((in[temp1]-out[temp1]==1&&out[temp2]-in[temp2]==1)||(in[temp2]-out[temp2]==1&&out[temp1]-in[temp1]==1)))
    102         {
    103             printf("Ordering is possible.\n");
    104             continue;
    105         }
    106         printf("The door cannot be opened.\n");
    107     }
    108     return 0;
    109 }
  • 相关阅读:
    如何一次插入多条记录的SQL语句
    oracle 创建联合主键语句
    02-36 支持向量回归
    245 第三篇:Django-路由控制
    244 第二篇:Django简介
    243 第一篇:自定义Web框架
    242 第一篇:Http协议详细介绍
    02-34 非线性支持向量机(鸢尾花分类)+自定义数据分类
    241 第一篇:web应用
    240 vue学习【第6篇】:vue之导入Bootstrap
  • 原文地址:https://www.cnblogs.com/XBWer/p/2655715.html
Copyright © 2020-2023  润新知