hdu 1181 变形课

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1181  

变形课

Description

呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规律:如果咒语是以a开头b结尾的一个单词,那么它的作用就恰好是使A物体变成B物体. 
Harry已经将他所会的所有咒语都列成了一个表,他想让你帮忙计算一下他是否能完成老师的作业,将一个B(ball)变成一个M(Mouse),你知道,如果他自己不能完成的话,他就只好向Hermione请教,并且被迫听一大堆好好学习的道理.

Input

测试数据有多组。每组有多行,每行一个单词,仅包括小写字母,是Harry所会的所有咒语.数字0表示一组输入结束.

Output

如果Harry可以完成他的作业,就输出"Yes.",否则就输出"No."(不要忽略了句号)

Sample Input

so
soon
river
goes
them
got
moon
begin
big
0

Sample Output

Yes.

建图,遍历。。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<queue>
 8 #include<map>
 9 using std::map;
10 using std::cin;
11 using std::cout;
12 using std::endl;
13 using std::find;
14 using std::sort;
15 using std::pair;
16 using std::queue;
17 using std::vector;
18 using std::multimap;
19 #define pb(e) push_back(e)
20 #define sz(c) (int)(c).size()
21 #define mp(a, b) make_pair(a, b)
22 #define all(c) (c).begin(), (c).end()
23 #define iter(c) decltype((c).begin())
24 #define cls(arr,val) memset(arr,val,sizeof(arr))
25 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
26 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
28 const int N = 26;
29 bool vis[N], G[N][N];
30 typedef unsigned long long ull;
31 void bfs(int s) {
32     cls(vis, false);
33     queue<int> q;
34     q.push(s);
35     vis[s] = true;
36     while (!q.empty()) {
37         int v = q.front(); q.pop();
38         rep(i, N) {
39             if (!G[v][i] || vis[i]) continue;
40             q.push(i);
41             vis[i] = true;
42         }
43     }
44     puts(vis['m' - 'a'] ? "Yes." : "No.");
45 }
46 int main() {
47 #ifdef LOCAL
48     freopen("in.txt", "r", stdin);
49     freopen("out.txt", "w+", stdout);
50 #endif
51     char buf[40];
52     while (~scanf("%s", buf)) {
53         if (buf[0] != '0') {
54             G[buf[0] - 'a'][buf[strlen(buf) - 1] - 'a'] = true;
55         } else {
56             bfs(1);
57             cls(G, false);
58         }
59     }
60     return 0;
61 }
View Code
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
原文地址:https://www.cnblogs.com/GadyPu/p/4649446.html