Play on Words
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8494 | Accepted: 2964 |
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.
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.".
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
题意:
给定一串单词 问其首位字母连接能否构成回路
代码:
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#define MAX 1002
#define N 27
using namespace std;
int point[N];
int tree[N];
char str[MAX];
int Find(int i)
{
if(i!=tree[i])
tree[i]=Find(tree[i]);
return tree[i];
}
void Make(int i)
{
if (tree[i]==-1)
tree[i]=i;
}
void Merge(int a,int b)
{
a=Find(a);
b=Find(b);
if (a!=b)
tree[a]=b;
}
int main()
{
int t,n,i,len;
scanf("%d",&t);
while(t--)
{
memset(point,0,sizeof(point));
memset(tree,-1,sizeof(tree));
scanf("%d",&n);
getchar();
while(n--)
{
scanf("%s",str);
len = strlen(str)-1;
int a=str[0]-'a'+1;
int b=str[len]-'a'+1;
point[a]++;
point[b]--;
Make(a);
Make(b);
Merge(a,b);
}
int in=0,out=0,word=0;
for (i=1;i<=26;i++)
{
if (point[i]==1) in++;
else if (point[i]==-1) out++;
else if (point[i]==0) word++;
}
if (!((in==1 && out==1 && word==24) || word==26)) //判断是否为欧拉通路或欧拉回路
{
printf("The door cannot be opened.\n");
continue;
}
int tmp;
for (i=1;i<=26;i++)
if(tree[i]!=-1)
{
tmp=i;
break;
}
int root=Find(tmp); //找到根节点
for (i=tmp;i<=26;i++)
{
if (tree[i]==-1) continue;
if (Find(i)!=root) //若存在点的节点不同于根节点 说明图不连通
{
printf("The door cannot be opened.\n");
break;
}
}
if (i==27)
printf("Ordering is possible.\n");
}
}
思路:
1. 有向图G为欧拉图,当且仅当G的基图(该有向图的无向图) 连通,且所有顶点的入度等于出度。
2. 有向图G为半欧拉图,当且仅当G的基图连通,且存在一个顶点的入度比出度大1, 一个顶点入度比出度小1,其它所有顶点的入度等于出度。
3. 无向图G为欧拉图,当且仅当G为连通的,且所有顶点度为偶数或者度为奇数的顶点个数为0或者2
4. 图的连通性可用并查集判断