HDU1116Play on Words

Play on Words

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


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
Central Europe 1999

欧拉回路加并查集

无向图是否具有欧拉通路或回路的判定:

欧拉通路:图连通;图中只有0个或2个度为奇数的节点

欧拉回路:图连通;图中所有节点度均为偶数

  (欧拉通路包含欧拉回路:当图中只有0个度为奇数时

有向图是否具有欧拉通路或回路的判定:

欧拉通路:图连通;①除2个端点外其余节点入度=出度,1个端点入度比出度大1,一个端点入度比出度小1 或② 所有节点入度等于出度

欧拉回路:图连通;所有节点入度等于出度

//有向图欧拉通路
#include<iostream>
#include<cstring>
using namespace std;
int map[30],out[30],in[30],vis[30];//map[i]存放父节点map[i]=j即i的父节点为j
//out[i]i点的出度,in[i]i点的入度 
int find(int x)
{
    int r=x;
    while(map[r]!=r)//mmp"!="不小心搞成了"=!"搞了两个小时了没发现这个错误 
    r=map[r];
    return r;
}
void join(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx!=fy)
    {
        map[fx]=fy; 
    }
}
int main()
{
    int T;
    string str;
    cin>>T;
    while(T--)
    {
        int n,a,b,count=0,count1=1;
        cin>>n;
        memset(out,0,sizeof(out));
        memset(in,0,sizeof(in));
        memset(vis,0,sizeof(vis));
        for(int i=1;i<30;i++)
            map[i]=i;
        
        for(int i=1;i<=n;i++)
        {
            cin>>str;
            int t=str.size();
            a=str[0]-'a'+1;
            b=str[t-1]-'a'+1;//提取首尾字符且转化为1~26中相应的数字 
            vis[a]=vis[b]=1;//标记出现的点 
            out[a]++;
            in[b]++;
            join(a,b);
        }
         int outnum=0,innum=0; 
        for(int i=1;i<30;i++)
        {
            if(vis[i])//1~26中有的数可能没有出现,我们只需要对出现的点进行判断即可 
            {
            if(map[i]==i)
               {
                count++;
                }
                if(out[i]!=in[i])
                {
                    if(out[i]-in[i]==1)
                    outnum++;
                    else
                    if(in[i]-out[i]==1)
                    innum++;
                    else
                    if(abs(in[i]-out[i])>1) 
                    count=0;//度数差值大于1是,count标记为0 
                 } 
            }
        } 
        if((count==1&&(innum==1&&outnum==1)&&count1)||(count==1&&(innum==0&&outnum==0)&&count1))
        cout<<"Ordering is possible."<<endl;
        else
        cout<<"The door cannot be opened."<<endl;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值