• POJ1386 Play on Words


    Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u

    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

    欧拉道路问题。这题欧拉回路或者欧拉道路都算成立。

    建好图以后先判一下连通性,如果图不连通,肯定不成立。

    ↑DFS BFS 并查集都可以

    然后是判欧拉路径。

      如果是欧拉回路,那么所有点的入度等于出度。

      如果是欧拉路径,那么只有两个点的入度不等于出度,其一是起始点,出度比入度大1,另一个是结束点,入度比出度大1

    之前一直WA,不知怎么改着改着就对了。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 using namespace std;
     7 const int mxn=1000;
     8 bool vis[mxn];
     9 int n;
    10 int fa[mxn];
    11 int in[mxn],out[mxn];
    12 void init(int n){
    13     for(int i=1;i<=n;i++)fa[i]=i;
    14 }
    15 int find(int x){
    16     if(fa[x]==x)return x;
    17     else return fa[x]=find(fa[x]);
    18 }
    19 char s[1100];
    20 int main(){
    21     int T;
    22     scanf("%d",&T);
    23     while(T--){
    24         memset(vis,0,sizeof vis);
    25         memset(in,0,sizeof in);
    26         memset(out,0,sizeof out);
    27         scanf("%d",&n);
    28         init(28);
    29         int i,j;
    30         for(i=1;i<=n;i++){
    31             scanf("%s",s);
    32             int u=s[0]-'a'+1;
    33             int v=s[strlen(s)-1]-'a'+1;
    34             in[v]++;out[u]++;
    35             vis[u]=vis[v]=1;
    36             int x=find(u),y=find(v);
    37             if(x!=y)fa[y]=x;
    38         }
    39         int st=0,ed=0;bool flag=0;
    40         int cnt=0;
    41         for(i=1;i<=26;i++){
    42             if(vis[i] && find(i)==i){
    43                 cnt++;
    44             }
    45             if(out[i]!=in[i]){//出度不等于入度时判定 
    46                 if(out[i]==in[i]+1){
    47                     if(!st)st=i;
    48                     else flag=1;
    49                 }
    50                 else if(in[i]==out[i]+1){
    51                     if(!ed)ed=i;
    52                     else flag=1;
    53                 }
    54                 else flag=1;
    55             }
    56         }
    57         if(cnt!=1){//非连通特判 
    58             printf("The door cannot be opened.
    ");
    59             continue;
    60             }
    61         if(flag==1)printf("The door cannot be opened.
    ");
    62         else printf("Ordering is possible.
    ");
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    MD5 Message Digest Algorithm in Visual Basic 6
    遍历指定文件夹的指定格式的文件并且copy到指定的文件夹下面
    宝宝的成长脚印8/17
    儿歌童谣两首
    office2003之資料編輯列的角字顯示7/10
    宝宝的成长脚印8/27
    宝宝的成长脚印7/30
    宝宝的成长脚印7/3
    LED手电筒真不咋滴8/27
    液晶显示器右边出现黑条7/7
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5665135.html
Copyright © 2020-2023  润新知