`
huobengluantiao8
  • 浏览: 1030777 次
文章分类
社区版块
存档分类
最新评论

poj_1386 Play on Words

 
阅读更多

Play on Words

Time Limit: 1000MS

Memory Limit: 10000K

Total Submissions: 7173

Accepted: 2519

题目链接:http://poj.org/problem?id=1386

Description

Some of the secret doorscontain a very interesting word puzzle. The team of archaeologists has to solveit to open that doors. Because there is no other way to open the doors, thepuzzle is very important for us.

There is a large number of magnetic plates on every door. Every plate has oneword written on it. The plates must be arranged into a sequence in such a waythat every word begins with the same letter as the previous word ends. Forexample, the word ``acm'' can be followed by the word ``motorola''. Your taskis to write a computer program that will read the list of words and determinewhether it is possible to arrange all of the plates in a sequence (according tothe given rule) and consequently to open the door.

Input

The input consists of T testcases. 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 Nthatindicates the number of plates (1 <= N <= 100000). Then exactly Nlinesfollow, each containing a single word. Each word contains at least two and atmost 1000 lowercase characters, that means only letters 'a' through 'z' willappear in the word. The same word may appear several times in the list.

Output

Your program has to determinewhether it is possible to arrange all the plates in a sequence such that thefirst letter of each word is equal to the last letter of the previous word. Allthe plates from the list must be used, each exactly once. The words mentionedseveral times must be used that number of times.
If there exists such an ordering of plates, your program should print thesentence "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

CentralEurope 1999

解题思路:

这是一个欧拉路的题目,求这种欧拉路径实现要先根据输入的第一个字母和最后一个字母来建图,然后用并查集来判断图的连通性(也可以用bfs),如果连通,在根据出度和入度判断地方是欧拉路,如果是就可以直接输出

代码:

#include <iostream>
#include<stdio.h>
#include<string.h>
#define MAX 27

using namespace std;

int g[MAX];
int indegree[MAX];
int outdegree[MAX];
int set[MAX];

void init()
{
	for(int i=0;i<MAX;i++)
	{
		set[i]=i;
	}
}

int findSet(int x)
{
	if(x!=set[x])
		set[x]=findSet(set[x]);
	return set[x];
}

void unionSet(int x,int y)
{
	int a=findSet(x);
	int b=findSet(y);
	if(a==b)
		return;
	if(set[a]<set[b])
	{
		set[b]=a;
	}
	else
	{
		set[a]=b;
	}
}

int main()
{
    int ts;
    int i;
    scanf("%d",&ts);
    while(ts--)
    {
        int num;
        memset(g,0,sizeof(g));
        memset(indegree,0,sizeof(indegree));
        memset(outdegree,0,sizeof(outdegree));
        scanf("%d",&num);
		init();
        for(i=0;i<num;i++)
        {
            char ch[1002];
			scanf("%s",ch);
            //gets(ch);
            int len=strlen(ch);
            int s=ch[0]-'a';
            int e=ch[len-1]-'a';
            g[s]=1;
            g[e]=1;
            //点s和点e的度数自加
            indegree[e]++;
            outdegree[s]++;
			//将点s和e连接起来
			unionSet(s,e);
        }
		int count1=0,count2=0;
		int flag=0;

		//判断图是否连通——变量一遍所有节点,如果每一个节点的父亲都是相同的,那么图是连通的
		for(i=0;i<MAX;i++)
		{/*
			if(g[i]==1)
			{
				count++;
				if(flag==0)
				{
					temp=findSet(i);
					flag=1;
				}
				else
				{
					if(temp!=findSet(i))
					{
						break;
					}
				}
			}
			*/
			//如果超过一个点的父亲是他本身,那么表示不连通
			if(g[i]==1 && set[i]==i)
			{
				flag++;
			}
		}

		if(flag>1)//图不连通
		{
			printf("The door cannot be opened.\n");
			continue;
		}

		//如果图连通,判读是否是欧拉路径
		/*无向图中,除源点和汇点为奇数除外,其余点的度数都为偶数,或者所有点的度数为偶数,那么存在欧拉路径*/
		for(i=0;i<MAX;i++)
		{
			if(g[i]==1)
			{
				if((indegree[i]!=outdegree[i]))
				{
					//如果出现OK  Ok这样一个词,那么就要判断如果出度减去入度大于1的话就不符合
					if((indegree[i]-outdegree[i])==1)
						count1++;
					else if((outdegree[i]-indegree[i])==1)
						count2++;
					else
						break;
				}
				
			}
		}
		if(i<MAX)
			printf("The door cannot be opened.\n");
		else if((count1+count2)==0 || (count1==1 && count2==1))
			printf("Ordering is possible.\n");
		else
			printf("The door cannot be opened.\n");

    }
    return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics