hdu 1181

dfs

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> using namespace std ; const int maxn = 1000 + 10 ; char s[maxn] , e[maxn] ; char str[maxn] ; int cnt , flag , vis[maxn] ; void dfs( int cur ) { // cout << s[cur] << " " << e[cur] << endl ; if( flag ) return ; if( e[cur] == 'm' ) { flag = 1 ; return ; } for( int i = 0 ; i < cnt ; ++i ) { if( s[i] == e[cur] && !vis[i] ) { vis[i] = 1 ; dfs( i ) ; } } return ; } int main() { cnt = 0 ; while( cin >> str ) { if( str[0] == '0' ) { for( int i = 0 ; i < cnt ; ++i ) if( s[i] == 'b' ) dfs( i ) ; if( flag ) cout << "Yes." << endl ; else cout << "No." << endl ; flag = 0 ; cnt = 0 ; } else { s[cnt] = str[0] ; e[cnt] = str[strlen(str)-1] ; cnt++ ; } } return 0 ; }

你可能感兴趣的:(hdu 1181)