Play on Words
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 N that indicates the number of
plates (1 ≤ N ≤ 100000). Then exactly N lines 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.’
欧拉回路的判断题;
对于有向图的欧拉道路判断,最好还是建无向图判断是否连通,因为有向图欧拉道路不一定就存在一个点的出度比入度大一,也就是说不一定存在起点,所以如果建有向图,可能会找不到起点,直接建无向图;
代码:
#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=200100;
const int M=401000;
const LL mod=998244353;
struct Node{
int to,nex;
}edge[M];
int cnt;
int head[30];
void add(int p,int q){
edge[cnt].to=q;
edge[cnt].nex=head[p];
head[p]=cnt++;
}
int in[30],out[30];
bool vis[30];
void dfs(int p){
vis[p]=true;
for(int i=head[p];~i;i=edge[i].nex){
if(!vis[edge[i].to]) dfs(edge[i].to);
}
}
void Init(){
memset(head,-1,sizeof(head));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
memset(vis,false,sizeof(vis));
cnt=0;
}
int main(){
ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--){
Init();
int n;
cin>>n;
string s;
for(int i=1;i<=n;i++){
cin>>s;
int u=s[s.length()-1]-'a';
int v=s[0]-'a';
in[u]++;
out[v]++;
add(v,u);
add(u,v);
}
int s1=0,s2=0,ss=0,d=0;
for(int i=0;i<26;i++){
if(in[i]||out[i]){
if(in[i]==out[i]+1) s1++;
if(out[i]==in[i]+1) s2++;
if(in[i]==out[i]) ss++;
d++;
}
}
if(ss==d-2&&s1==1&&s2==1||ss==d&&s1==0&&s2==0){
dfs(s[0]-'a');
int ok=0;
for(int i=0;i<26;i++){
if(in[i]||out[i]){
if(!vis[i]){
ok=1;
break;
}
}
}
if(ok) cout<<"The door cannot be opened."<<endl;
else cout<<"Ordering is possible."<<endl;
}
else cout<<"The door cannot be opened."<<endl;
}
return 0;
}
1052

被折叠的 条评论
为什么被折叠?



