#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
string str;
int father[30];
void init() {
for(int i = 0; i < 30; i++) {
father[i] = i;
}
}
int get(int x) {
if(father[x] == x) {
return x;
}
father[x] = get(father[x]);
return father[x];
}
void merge(int x, int y) {
x = get(x);
y = get(y);
if(x != y) {
father[y] = x;
}
}
int main() {
int i;
while(cin>>str) {//难在多组数据的处理
init();
int len = str.length();
int x = get(str[0]-'a');
int y = get(str[len-1]-'a');
if((x != y) && (y != 'b'-'a'))//这个条件很重要,因为b是开头
merge(x,y);
while(cin>>str) {
if(str[0] == '0')
break;
len = str.length();
x = get(str[0]-'a');
y = get(str[len-1]-'a');
if((x != y) && (y != 'b'-'a'))//这个条件很重要,因为b是开头
merge(x,y);
}
x=get('m'-'a');
y=get('b'-'a');
if(x != y)
cout<<"No."<<endl;
else
cout<<"Yes."<<endl;
}
return 0;
}
HDU 1181 变形课(并查集)
最新推荐文章于 2019-03-19 20:04:31 发布