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. 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.
题意:读入一组单词,然后判定单词是否可以通过重组使得每个单词第一个字母跟前一个单词最后一个字母相同
分析:把每个字母看成结点,单词看成有向边,然后就是有判断向图是否含有欧拉通。
路。
有向图欧拉回路:
(1)有向图的底图连通。(可用并查集判断)
(2)最多有两个点入度跟出度不同。
(3)有两个点不同时,必须一个点入度比出度大1,一个出度比入度大1。
满足上述条件,无向图有欧拉通路。
#include<iostream> #include<cstdio> #include<cctype> #include<cstring> using namespace std; const int N=1010; inline int read(){ int sum=0,f=1; char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch)){sum=sum*10+ch-'0';ch=getchar();} return sum*f; } int n; char s[N]; int pre[26],a[26],b[26],c[26]; int Find(int x){ if(x!=pre[x]) pre[x]=Find(pre[x]); return pre[x]; } void join(int x,int y){ int tx=Find(x); int ty=Find(y); if(tx!=ty){ pre[ty]=tx; } } void init(){ for(int i=0;i<26;i++){ pre[i]=i; } } int main(){ //freopen("in.txt","r",stdin); int t; t=read(); while(t--){ memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); n=read(); init(); for(int i=1;i<=n;i++){ scanf("%s",s); int len=strlen(s); a[s[0]-'a']++; b[s[len-1]-'a']++; if(Find(s[0]-'a')!=Find(s[len-1]-'a')){ join(s[0]-'a',s[len-1]-'a'); } } bool flag=false; int cnt=0,cnt1=0; for(int i=0;i<=25;i++){ if(pre[i]!=i&&(a[i]+b[i])){ cnt1++; } if(a[i]+b[i]) cnt++; } if(cnt!=cnt1+1) flag=true; cnt=0; for(int i=0;i<26;i++){ if(a[i]!=b[i]){ c[cnt++]=a[i]-b[i]; } if(cnt>2){ flag=true; break; } } if(cnt==2){ if(!(c[0]==-1&&c[1]==1||c[0]==1&&c[1]==-1)) flag=true; } if(flag) printf("The door cannot be opened. "); else printf("Ordering is possible. "); } return 0; }