欧拉通路-Play on Words 分类: POJ 图论 2015-08-06 19:13 4人阅读 评论(0) 收藏
Play on Words
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10620 Accepted: 3602
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
题意:给你n个字符串,判断这些字符串能连接在一起使的每个单词的第一个字母与前面一个的字符串的最后一个字母相同.
方法:并查集+欧拉通路,并查集判断图是不是连通的.用有向图的欧拉路的性质判断.能不能形成欧拉通路.
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define RR freopen("input.txt","r",stdin)
//#pragma comment(linker, "/STACK:102400000")
#define WW freopen("output.txt","w",stdout)
const int MAX = 1100;
char s[MAX];
bool vis[26];
int pre[26];
int in[26],out[26];
int Find(int x)//并查集
{
return pre[x]==-1?x:pre[x]=Find(pre[x]);
}
void Link(int x,int y)
{
int Fx=Find(x);
int Fy=Find(y);
if(Fx!=Fy)
{
pre[Fx]=Fy;
}
}
bool Judge()
{
int ood=0,ooc=0;
int ans=-1;
for(int i=0;i<26;i++)
{
if(!vis[i])
{
continue;
}
if(in[i]-out[i]>=2)//如果入度与出度的差值大于1,则形不成欧拉通路.
{
return false;
}
if(out[i]-in[i]>=2)
{
return false;
}
if(in[i]-out[i]==1)
{
ood++;
if(ood>1)//只有一个顶点的入度与出度的差值为一,另一个的出度与入度的差值为一,其余的差值为零.
{
return false;
}
}
if(out[i]-in[i]==1)
{
ooc++;
if(ooc>1)
{
return false;
}
}
if(out[i]==0&&in[i]==0)
{
return false;
}
if(ans==-1)
{
ans=i;
}
else
{
if(Find(i)!=Find(ans))//判断图是不是连通的.
{
return false;
}
}
}
if(ood!=ooc)
{
return false;
}
return true;
}
int main()
{
int T;
int n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(vis,false,sizeof(vis));
memset(pre,-1,sizeof(pre));
memset(out,0,sizeof(out));
memset(in,0,sizeof(in));
for(int i=0;i<n;i++)
{
scanf("%s",s);
int len=strlen(s);
in[s[len-1]-'a']++;
out[s[0]-'a']++;
Link(s[0]-'a',s[len-1]-'a');
vis[s[0]-'a']=true;
vis[s[len-1]-'a']=true;
}
if(Judge())
{
printf("Ordering is possible.\n");
}
else
{
printf("The door cannot be opened.\n");
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。