UVa 10129 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 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.
题意
给你n个字符串,判断是否能接成一串(上一串的尾==下一串的头)
题解
判断有向图是否成欧拉路径需要2个条件
1.当前图连通(这里可以用并查集,可以用dfs)
2.最多只能有2个点入度In!=出度Out,而且必须是其中1个点出度=入度+1(起点),另1个点入度=出度+1才行(终点)
代码
1 #include<bits/stdc++.h> 2 using namespace std; 3 int F[26]; 4 int Find(int x) 5 { 6 return F[x]==x?x:F[x]=Find(F[x]); 7 } 8 void Join(int x,int y) 9 { 10 if(F[x]==-1)F[x]=x; 11 if(F[y]==-1)F[y]=y; 12 int fx=Find(x); 13 int fy=Find(y); 14 if(fx!=fy) 15 F[fx]=fy; 16 } 17 int main() 18 { 19 int n,T; 20 char s[1005]; 21 scanf("%d",&T); 22 while(T--) 23 { 24 int In[26]={0},Out[26]={0}; 25 memset(F,-1,sizeof(F)); 26 scanf("%d",&n); 27 for(int i=0;i<n;i++) 28 { 29 scanf("%s",s); 30 int l=strlen(s); 31 int a=s[0]-'a',b=s[l-1]-'a'; 32 In[a]++;Out[b]++; 33 Join(a,b); 34 } 35 int flag=1,flag1=0,flag2=0,flag3=0; 36 for(int i=0;i<26;i++)//图连通 37 if(F[i]!=-1&&F[i]==i) 38 if(flag3==0)flag3=1; 39 else flag=0; 40 for(int i=0;i<26;i++) 41 { 42 int h=In[i],t=Out[i]; 43 if(h==t)//入度=出度 44 continue; 45 else if(h-t==1)//入度=出度+1 46 if(flag1==1)flag=0; 47 else flag1=1; 48 else if(t-h==1)//出度=入度+1 49 if(flag2==1)flag=0; 50 else flag2=1; 51 else 52 flag=0; 53 } 54 if(flag)printf("Ordering is possible.\n"); 55 else printf("The door cannot be opened.\n"); 56 } 57 return 0; 58 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库