题目链接

题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同。给你一组单词问能不能排成上述形式。

思路 :把每个单词看成有首字母指向尾字母的有向边,每个字母看成一个点,题中要求等效于判断图中是否存在一条路径经过每一条一次且仅一次,就是有向欧拉通路。统计个顶点的出入度,如果每个点的出入度都相同,那就是欧拉回路,如果有两个奇数度,那就是欧拉通路,除此之外,都不能满足要求。还有别忘了判断是否连通,此时用到并查集,图中所有的边(u,v),如果u!=v且属于不同的连通分量,就合并。欧拉回路的基本定理及概念。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4.  
  5. using namespace std ;
  6.  
  7. char word[] ;
  8. int father[],out[],in[] ,vis[];
  9.  
  10. int find_(int x)
  11. {
  12. if(father[x] != x)
  13. father[x] = find_(father[x]) ;
  14. return father[x] ;
  15. }
  16.  
  17. void mergee(int a,int b)
  18. {
  19. if(find_(a) != find_(b))
  20. father[find_(a)] = find_(b) ;
  21. }
  22.  
  23. void Init()
  24. {
  25. memset(out,,sizeof(out)) ;
  26. memset(in,,sizeof(in)) ;
  27. for(int i = ; i < ; i++)
  28. father[i] = i ;
  29. memset(vis,,sizeof(vis)) ;
  30. }
  31.  
  32. int main()
  33. {
  34. int T ;
  35. cin >> T ;
  36. int n ;
  37. while(T--)
  38. {
  39. cin >> n ;
  40. Init() ;
  41. while(n -- )
  42. {
  43. scanf("%s",word) ;
  44. int u = word[]-'a' ;
  45. int v = word[strlen(word)-]-'a' ;
  46. mergee(u,v) ;
  47. out[u] ++ ;
  48. in[v] ++ ;
  49. vis[u] = vis[v] = ;
  50. }
  51. int cnt = ,cnt1 = ,cnt2 = ;
  52. for(int i = ; i < ; i++)
  53. {
  54. if(vis[i] && father[i] == i)
  55. {
  56. cnt ++ ;
  57. }
  58. }
  59. if(cnt > )
  60. {
  61. puts("The door cannot be opened.") ;
  62. continue ;
  63. }
  64. bool flag = true ;
  65. for(int i = ; i < ; i++)
  66. {
  67. if(vis[i] && out[i] != in[i])
  68. {
  69. if(out[i]-in[i] == )
  70. {
  71. cnt1 ++ ;
  72. if(cnt1 > )
  73. {
  74. flag = false ;
  75. break ;
  76. }
  77. }
  78. else if(in[i]-out[i] == )
  79. {
  80. cnt2 ++ ;
  81. if(cnt2 > )
  82. {
  83. flag = false ;
  84. break ;
  85. }
  86. }
  87. else
  88. {
  89. flag = false ;
  90. break ;
  91. }
  92. }
  93. }
  94. if(!flag) puts("The door cannot be opened.") ;
  95. else puts("Ordering is possible.") ;
  96. }
  97. return ;
  98. }

HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)的更多相关文章

  1. poj 1386 Play on Words 有向欧拉回路

    题目链接:http://poj.org/problem?id=1386 Some of the secret doors contain a very interesting word puzzle. ...

  2. hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)

    Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...

  3. poj 1386 Play on Words门上的单词【欧拉回路&&并查集】

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

  4. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

  5. HDU 1116 Play on Words(欧拉回路+并查集)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1116 Play on Words Time Limit: 10000/5000 MS (Java/Ot ...

  6. 【POJ 1988】 Cube Stacking (带权并查集)

    Cube Stacking Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)id ...

  7. 【POJ 1984】Navigation Nightmare(带权并查集)

    Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...

  8. POJ 1703 Find them, Catch them (数据结构-并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31102   Accepted: ...

  9. HDU 3038 - How Many Answers Are Wrong - [经典带权并查集]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

随机推荐

  1. hdu 5327 Olympiad

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5327 Olympiad Description You are one of the competit ...

  2. lvs keepalived 安装配置详解

    前段时间看了一篇文章,lvs做负载均衡根F5差不多,说实话不怎么相信,因为F5没玩过,也无法比较.F5相当的贵,真不是一般企业能负担的起的.负载均衡软件也用过不少,nginx,apache,hapro ...

  3. 67.ARP协议

    ARP是地址解析协议Address Resolution Protocol的缩写.是一个位于TCP/IP协议栈中的低层协议,负责将某个IP地址解析程对应的MAC地址.在局域网中,网络实际传输的是“帧” ...

  4. xml数据读 swift

    // // ViewController.swift // xml读写 // // Created by mac on 15/7/14. // Copyright (c) 2015年 fangyuha ...

  5. Notes of the scrum meeting(12.8)

    meeting time:18:00~18:30p.m.,December 8th,2013 meeting place:20号公寓前 attendees: 顾育豪                   ...

  6. 1491: [NOI2007]社交网络 - BZOJ

    Description Input Output输出文件包括n 行,每行一个实数,精确到小数点后3 位.第i 行的实数表 示结点i 在社交网络中的重要程度.Sample Input4 41 2 12 ...

  7. 【BZOJ】【1053】【HAOI2007】反素数ant

    搜索 经典搜索题目(其实是蒟蒻只会搜……vfleaking好像有更优秀的做法?) 枚举质数的幂,其实深度没多大……因为$2^32$就超过N了……而且质数不能取的太大,所以不会爆…… /******** ...

  8. Poj 2349 Arctic Network 分类: Brush Mode 2014-07-20 09:31 93人阅读 评论(0) 收藏

    Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9557   Accepted: 3187 De ...

  9. php正则过滤html标签、空格、换行符的代码,提取图片

    $descclear = str_replace("r","",$descclear);//过滤换行 $descclear = str_replace(&quo ...

  10. C语言中inline的用法

    C语言里面的内联函数(inline)与宏定义(#define)探讨 先简明扼要,说下关键: 1.内联函数在可读性方面与函数是相同的,而在编译时是将函数直接嵌入调用程序的主体,省去了调用/返回指令,这样 ...