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

    Problem UVA10129-Play on Words

    Accept: 2534  Submit: 19477

    Time Limit: 3000 mSec

    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 N that indicates the number of plates (1 ≤ N ≤ 100000). Then exactly N lines 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.

    题解:欧拉路径模板题,这里面运用并查集判断联通,对于欧拉路径的判断,lrj的代码给了一个很好的模板,简单严密

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <vector>
     6 using namespace std;
     7 
     8 const int maxl = 1000+10;
     9 const int kind = 26;
    10 char str[maxl];
    11 int n,pre[kind],deg[kind];
    12 bool used[kind];
    13 
    14 int findn(int x){
    15     return x == pre[x] ? x : pre[x] = findn(pre[x]);
    16 }
    17 
    18 int main()
    19 {
    20     //freopen("input.txt","r",stdin);
    21     int iCase;
    22     scanf("%d",&iCase);
    23     while(iCase--){
    24         scanf("%d",&n);
    25         memset(used,false,sizeof(used));
    26         memset(deg,0,sizeof(deg));
    27         for(int i = 0;i < 26;i++) pre[i] = i;
    28         int cnt = 26;
    29         for(int i = 1;i <= n;i++){
    30             scanf("%s",str);
    31             int x1 = str[0]-'a',x2 = str[strlen(str)-1]-'a';
    32             used[x1] = used[x2] = true;
    33             deg[x1]++,deg[x2]--;
    34             int fx1 = findn(x1),fx2 = findn(x2);
    35             if(fx1 != fx2){
    36                 pre[fx1] = fx2;
    37                 cnt--;
    38             }
    39         }
    40         vector<int> ans;
    41         for(int i = 0;i < kind;i++){
    42             if(!used[i]) cnt--;
    43             else if(deg[i] != 0){
    44                 ans.push_back(deg[i]);
    45             }
    46         }
    47         bool ok = false;
    48         if(cnt==1 && (ans.empty() || (ans.size()==2) && (ans[0]==1 || ans[0]==-1))) ok = true;
    49         if(ok) printf("Ordering is possible.
    ");
    50         else printf("The door cannot be opened.
    ");
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    [原]在Solaris 10/09上静默安装和升级Oracle 10g和Oracle 11g(一)
    [原]不祥的CPU——Alpha
    [原]单核、双核、四核、八核、十六核......(更新于2010年7月11日,增加了32核的截图)
    [原]记一次处理Oracle死会话的过程
    [原]记一次使用flashback恢复数据,警惕自己不要浮躁,还是太嫩了
    在Solaris中如何解开tar.gz的文件
    说说Oracle的rowid
    [原]SQL解决“俯瞰金字塔”矩阵
    百度搜索支持手写输入法,我爸妈终于可以用上搜索引擎了
    [原] 在Solaris 10/09上静默安装和升级Oracle 10g和Oracle 11g(四)
  • 原文地址:https://www.cnblogs.com/npugen/p/9513163.html
Copyright © 2011-2022 走看看