POJ1386 Play on Words 有向图欧拉通路判断

题目中给你n个字符串,问你能不能找到一种排列方案,使前一个单词的尾部字符是下一个单词的首部字母。


这里我们要将问题转化,将一个单词看成一条边。将首尾两个字符分别看做两个节点,中间字母全部忽略

比如数据1:

acm
ibm

 acm  是一条  a->m的边

ibm    是一条   i->m的边

判断存不存一条欧拉通路能够遍历每一条边,注上述问题的单词已经被我转化为一条有向边,然后前一个单词的尾部字符是下一个单词的首部字母这一条件就是两个单词的共同节点。判断有向图欧拉回路直接统计每一个节点的出入度即可。


注意点:

图可能不连通,要用并查集判断,判断方法就是将一条边上的两个点连接,最后判断所有点是不是一个集合内即可。



Play on Words
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8012 Accepted: 2817

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.

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<utility>

using namespace std;

vector< pair<int,int> > edge;
int set[30];
int used[30];
int in[30],out[30];
int n;

void init()
{
    memset(used,0,sizeof(used));
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
	for(int i=0;i<=26;i++)
		set[i]=i;
    edge.clear();
}

int find(int a)
{

	int root=a;
	int temp;
	while(set[root]!=root)
		root=set[root];
	while(set[a]!=root)
	{
		temp=a;
		a=set[a];
		set[temp]=root;
	}
	return root;
}

bool merge(int a,int b)
{
	int x=find(a);
	int y=find(b);
	if(x==y)
		return false;
	set[x]=y;
	return true;
}

bool eluer()
{
    int ort,st,ed;
    st=ed=0;
    ort=-1;
    for(int i=0;i<edge.size();i++)
       merge(find(edge[i].first),find(edge[i].second));
    for(int i=0;i<26;i++)
        if(used[i])
        {
            if(ort==-1) ort=i;
            if(abs(in[i]-out[i])>=2)
                return false;
            if(find(i)!=find(ort))
                return false;
            if(in[i]-out[i]==1)
            {
                ed++;
                if(ed>1)
                    return false;
             }
             if(out[i]-in[i]==1)
             {
                 st++;
                 if(st>1)
                    return false;
             }
        }
    return true;
}

char s[1100];

int main()
{
    int t;
    scanf("%d",&t);
    for(int kk=0;kk<t;kk++)
    {
        scanf("%d",&n);
        init();
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            int u=s[0]-'a';
            int v=s[strlen(s)-1]-'a';
            in[v]++,out[u]++;
            used[v]=used[u]=1;
            edge.push_back(make_pair(v,u));
        }
        if(!eluer())
            printf("The door cannot be opened.\n");
        else
            printf("Ordering is possible.\n");

    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值