uva 10129 poj 1386 hdu 1116 zoj 2016 play on words

//本来是想练一下欧拉回路的,结果紫书上那题是大水题!!!!!

题意:给出n个单词,是否可以把单词排列成每个单词的第一个字母和上一个单词的最后一个字母相同

解:欧拉通路存在=底图联通+初度!=入度的点至多只有两个(分别为入点和出点)

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<queue>
 8 #include<vector>
 9 #include<map>
10 #include<stack>
11 #include<string>
12
13 using namespace std;
14
15 int T,n;
16 int in[26],out[26];
17 int f[26];
18 char s[1007];
19
20 int gf(int x){
21     if (x==f[x]) return f[x];
22     f[x]=gf(f[x]);
23     return f[x];
24 }
25
26 bool solve(){
27             memset(in,0,sizeof(in));
28             memset(out,0,sizeof(out));
29             for (int i=0;i<26;i++){
30                     f[i]=i;
31             }
32             scanf("%d",&n);
33             for (int i=0;i<n;i++){
34                     scanf("%s",s);
35                     int f1=gf(s[0]-‘a‘);
36                     int f2=gf(s[strlen(s)-1]-‘a‘);
37                     if (f1!=f2){
38                             f[f1]=f2;
39                     }
40                     in[s[0]-‘a‘]++;
41                     out[s[strlen(s)-1]-‘a‘]++;
42             }
43             int pre=-1;
44             for (int i=0;i<26;i++){
45                     if (in[i]!=0 || out[i]!=0){
46                             if (pre==-1)
47                                 pre=gf(i);
48                             else{
49                                     if (pre!=gf(i)) return false;
50                                     pre=gf(i);
51                             }
52                     }
53             }
54             for (int i=0;i<26;i++){
55                     in[i]=in[i]-out[i];
56             }
57             sort(in,in+26);
58             for (int i=1;i<25;i++) if (in[i]!=0) return false;
59             if (abs(in[0])>1 || abs(in[25])>1 || in[0]+in[25]!=0) return false;
60             if (in[0]+in[25]!=0) return false;
61             return true;
62 }
63
64 int main(){
65     scanf("%d",&T);
66     for (int cas=1;cas<=T;cas++){
67             if (solve())
68                 printf("Ordering is possible.\n");
69             else
70                 printf("The door cannot be opened.\n");
71     }
72     return 0;
73 }
74 /*
75 3
76 2
77 acm
78 ibm
79 3
80 acm
81 malform
82 mouse
83 2
84 ok
85 ok
86 */
时间: 2024-10-29 00:31:04

uva 10129 poj 1386 hdu 1116 zoj 2016 play on words的相关文章

POJ 1071 &amp; HDU 1364 &amp; ZOJ 1019 Illusive Chase(DFS)

题目链接: POJ:http://poj.org/problem?id=1071 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1364 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=19 Description Tom the robocat is presented in a Robotics Exhibition for an enthusiastic audien

poj 1543 &amp; HDU 1334 &amp; ZOJ 1331 Perfect Cubes(数学 暴力大法好)

题目链接:http://poj.org/problem?id=1543 Description For hundreds of years Fermat's Last Theorem, which stated simply that for n > 2 there exist no integers a, b, c > 1 such that a^n = b^n + c^n, has remained elusively unproven. (A recent proof is believ

POJ 2062 HDU 1528 ZOJ 2223 Card Game Cheater

水题,感觉和田忌赛马差不多 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; struct P1 { int Num; int Hua; } Play1[30]; struct P2 { int Num; int Hua; } Play2[30]; bool cmp1(const P1&a,const P1&b

UVA 11368 &amp; POJ 3636 &amp; HDU 1677 Nested Dolls(贪心 + 二分LIS)

A - Nested Dolls Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Dilworth is the world's most prominent collector of Russian nested dolls: he literally has thousands of them! You know, the wooden hollow dolls

HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)

题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有首字母指向尾字母的有向边,每个字母看成一个点,题中要求等效于判断图中是否存在一条路径经过每一条一次且仅一次,就是有向欧拉通路.统计个顶点的出入度,如果每个点的出入度都相同,那就是欧拉回路,如果有两个奇数度,那就是欧拉通路,除此之外,都不能满足要求.还有别忘了判断是否连通,此时用到并查集,图中所有的边

POJ1607 &amp; HDU 1330 &amp; ZOJ 1216 Deck(数学题)

题目链接: POJ  1607 : http://poj.org/problem?id=1607 HDU 1330 :http://acm.hdu.edu.cn/showproblem.php?pid=1330 ZOJ  1216 : Description A single playing card can be placed on a table, carefully, so that the short edges of the card are parallel to the table

UVA 124 &amp; POJ 1270 Following Orders(拓扑排序)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=60 http://poj.org/problem?id=1270 Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3806   Accepted: 1507 Description Or

POJ 2411 &amp;&amp; HDU 1400 Mondriaan&#39;s Dream (状压dp 经典题)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12341   Accepted: 7204 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

HDU 1986 &amp; ZOJ 2989 Encoding(模拟)

题目链接: HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1986 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1988 HDU 1987 & ZOJ 2990 和这题刚好相反,也是比较容易模拟: Chip and Dale have devised an encryption method to hide their (written) text messages