标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 20748 Accepted Submission(s): 7494
#include<cstdio> #include<cstring> #include<algorithm> #include<math.h> #include<queue> #include<iostream> using namespace std; typedef long long LL; const int INF = 999999999; struct Node { char s[2],e[2]; } node[1000]; int graph[1000][1000]; char s[1000][100]; int main() { int id,id1,id2; int a[1000],b[1000]; int t = 0; while(scanf("%s",s[t])!=EOF) { if(s[t][0]==‘0‘) { id = 0,id1=0,id2=0; for(int i=0; i<t; i++) { int len = strlen(s[i]); node[id].s[0] = s[i][0]; node[id].e[0] = s[i][len-1]; if(s[i][0]==‘b‘) a[id1++] = id; if(s[i][len-1]==‘m‘) b[id2++] = id; id++; } for(int i=0; i<id; i++) { for(int j=0; j<id; j++) { if(i==j) graph[i][j] = 0; else graph[i][j] =INF; } } for(int i=0; i<id; i++) { for(int j=0; j<id; j++) { if(i!=j) if(node[i].e[0]==node[j].s[0]) graph[i][j] = 1; } } for(int k=0; k<id; k++) /// floyed { for(int i=0; i<id; i++) { for(int j=0; j<id; j++) { graph[i][j] = min(graph[i][j],graph[i][k]+graph[k][j]); } } } bool flag = false; for(int i=0; i<id1&&!flag; i++) { for(int j=0; j<id2; j++) { if(graph[a[i]][b[j]]!=INF) { flag = true; break; } } } if(flag) printf("Yes.\n"); else printf("No.\n"); t = 0; }else t++; } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5572644.html