Play on Words(欧拉回路)
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.
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 int par[26]; 6 int id[26]; 7 int od[26]; ///par为并查集,id为入度,od为出度 8 int vis[26]; ///vis为该顶点出现过的标记 9 int finds(int x)///并查集的查找函数 10 { 11 int r = x; 12 while(r != par[r]) 13 { 14 r = par[r]; 15 } 16 int i = x, j; 17 while(i != r)///路径压缩 18 { 19 j = par[i]; 20 par[i] = r; 21 i = j; 22 } 23 return r; 24 } 25 void join(int x, int y)///并查集的合并函数 26 { 27 int fx = finds(x); 28 int fy = finds(y); 29 if(fx != fy)///如果x,y在两个不同的连通分量上,那么合并 30 { 31 par[fy] = fx; 32 } 33 } 34 int main() 35 { 36 int t, n, i; 37 int u,v; 38 char str[1005]; 39 scanf("%d", &t); 40 while(t--) 41 { 42 memset(id, 0, sizeof(id));///初始化各个数组 43 memset(od, 0, sizeof(od)); 44 memset(vis, false, sizeof(vis)); 45 for(i = 0; i <26; i++)///初始化并查集,根节点为自身 46 { 47 par[i] = i; 48 } 49 scanf("%d", &n); 50 while(n--) 51 { 52 scanf("%s", str); 53 int len = strlen(str); 54 u=str[0]-'a'; 55 v=str[len-1]-'a'; 56 join(u,v);///合并出现的顶点 57 id[v]++;///相应的入度+1 58 od[u]++;///相应的出度+1 59 vis[u]=vis[v]=1;///标记该顶点出现过 60 } 61 int flag = 0, tag = 0, a = 0, b = 0; ///flag来判图是否连通,tag为图中顶点入度不等于出度的个数 62 ///a为入度大出度1的点的个数,b为出度大入度1的点的个数 63 for(i = 0; i <26; i++)///判连通 64 { 65 if(vis[i] && par[i] == i)///如果连通,那么所有的点将被划分到一个集合之中 66 { 67 flag++; 68 } 69 } 70 if(flag > 1)///不连通,直接输出 71 { 72 printf("The door cannot be opened.\n"); 73 } 74 else///flag必须为1 75 { 76 for(i = 0; i <26; i++)///查找tag,a, b 的个数 77 { 78 if(vis[i] && id[i] != od[i]) 79 { 80 tag++; 81 } 82 if(vis[i] && id[i]-od[i] == 1) 83 { 84 a++; 85 } 86 if(vis[i] && od[i]-id[i] == 1) 87 { 88 b++; 89 } 90 } 91 if(tag == 0)///tag = 0,对于所有的点入度等于初度,存在欧拉回路 92 { 93 printf("Ordering is possible.\n"); 94 } 95 else if(a == b && a == 1 && tag == 2)///a = 1 && b = 1 && tag = 2.存在欧拉通路 96 { 97 printf("Ordering is possible.\n"); 98 } 99 else 100 { 101 printf("The door cannot be opened.\n"); 102 } 103 } 104 } 105 return 0; 106 }
本文作者:王陸
本文链接:https://www.cnblogs.com/wkfvawl/p/9627986.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
· SQL Server统计信息更新会被阻塞或引起会话阻塞吗?
· 本地部署 DeepSeek:小白也能轻松搞定!
· 传国玉玺易主,ai.com竟然跳转到国产AI
· 自己如何在本地电脑从零搭建DeepSeek!手把手教学,快来看看! (建议收藏)
· 我们是如何解决abp身上的几个痛点
· 如何基于DeepSeek开展AI项目