zoukankan      html  css  js  c++  java
  • UVa 10129 Play on Words(并查集+欧拉路径)

    题目链接:

    https://cn.vjudge.net/problem/UVA-10129

    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 Specification

    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 Specification

    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

    Output for the Sample Input

    The door cannot be opened.Ordering is possible.The door cannot be opened.
     1 /*
     2 题意描述:
     3 输入给出n个单词,问是否能够构成一个序列
     4 解题思路:
     5 使用并查集判断图是否连通,再判断是否满足有向图存在欧拉道路的条件
     6 
     7 直观的想法是把每个单词看成一个节点,如果两个符合首尾相连的条件就建立一种关系,然后判断是否能够构成一个序列,
     8 但是把单词成一个节点,使用并查集时,时间复杂度是n方,而数据范围是10万,肯定会超时的。
     9 不妨直接将每个单词看成一种关系,也就是首字母和尾字母是节点,每个单词是一种关系。
    10 这样使用并查集和并的时候就是n的复杂度了。 
    11 易错分析:
    12 注意判断是否满足欧拉道路的条件时,要分两种情况 
    13 */ 
    14 #include<bits/stdc++.h>
    15 
    16 int rd[26],cd[26],f[26],bk[26];
    17 int n;
    18 void merge(int u,int v);
    19 int getf(int v);
    20 int check();
    21 
    22 int main()
    23 {
    24     //freopen("E:\testin.txt","r",stdin);//记得写路径 
    25     int T;
    26     char word[1010];
    27     int fa,la;
    28     scanf("%d",&T);
    29     while(T--){
    30         scanf("%d",&n);
    31         for(int i=0;i<26;i++){
    32             bk[i]=0;
    33             f[i]=i;
    34             cd[i]=rd[i]=0;
    35         }
    36         for(int i=0;i<n;i++){
    37             scanf("%s",word);
    38             fa=word[0]-'a';
    39             bk[fa]=1;
    40             cd[fa]++;
    41             la=word[strlen(word)-1]-'a';
    42             rd[la]++;
    43             bk[la]=1;
    44             
    45             merge(fa,la);
    46         }
    47         
    48         if(check())
    49             printf("Ordering is possible.
    ");
    50         else
    51             printf("The door cannot be opened.
    ");
    52     }
    53     return 0;
    54 }
    55 
    56 int check(){
    57     int sum=0;
    58     for(int i=0;i<26;i++){
    59         if(f[i] == i && bk[i])
    60             sum++;
    61     }    
    62     if(sum > 1)    
    63         return 0;
    64     
    65     sum=0;
    66     int q=0,z=0;
    67     for(int i=0;i<26;i++){
    68         if(bk[i]){
    69             if(cd[i] != rd[i]){
    70                 sum++;
    71                 if(cd[i] - 1 == rd[i])
    72                     q++; 
    73                 if(rd[i] - 1 == cd[i])    
    74                     z++; 
    75             }
    76         }
    77     }
    78     //存在欧拉道路的两种情况 
    79     if((sum == 2 && q == 1 && z == 1) || (sum == 0 && q == 0 && z == 0)) return 1;
    80     else return 0;
    81 }
    82 void merge(int u,int v){
    83     int t1=getf(u);
    84     int t2=getf(v);
    85     if(t1 != t2){
    86         f[t2]=t1;
    87     }
    88 }
    89 int getf(int v){
    90     return f[v]==v ? v : f[v]=getf(f[v]);
    91 }
  • 相关阅读:
    informix数据库的日志
    javaScript之BOM操作2
    javaScript之BOM操作1
    开发必会系列:hibernate事务
    性能测试系列:Oracle数据库awr报告使用与分析
    《股票大作手回忆录》读书笔记
    金融知识学习综合笔记
    开发必会系列:《深入理解JVM(第二版)》读书笔记
    基础教材系列:计算机底层知识点积累
    基础教材系列:数据结构与算法——慕课网笔记
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/9327244.html
Copyright © 2011-2022 走看看