思路:将每个关系当成一条有向边,查询时就判断之间存在路径。
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<map> #define Maxn 210 using namespace std; map<string,int> g; map<string,int> ver; int head[Maxn*2],vi[Maxn*2],e; struct Edge{ int u,v,next; }edge[Maxn*Maxn*2]; void add(int u,int v){ edge[e].u=u,edge[e].v=v,edge[e].next=head[u],head[u]=e++; } bool dfs(int u,int x) { int i,v; vi[u]=1; if(u==x) return true; for(i=head[u];i!=-1;i=edge[i].next){ v=edge[i].v; if(vi[v]) continue; if(dfs(v,x)) return true; } return false; } int main() { char str[101001],s1[Maxn],s2[Maxn],s3[Maxn],s4[Maxn],s5[Maxn],s6[Maxn]; int cnt,num,i,j,t,Ca=0,u,v; scanf("%d",&t); getchar(); while(t--){ cnt=0; memset(vi,0,sizeof(vi)); memset(head,-1,sizeof(head)); e=0; g.clear(); ver.clear(); printf("Case #%d: ",++Ca); while(cin.getline(str,2000,' ')){ int l=strlen(str); if(str[l-1]=='!'){ printf(" "); break; } num=sscanf(str,"%s %s %s %s %s %s",s1,s2,s3,s4,s5,s6); if(num==3){ l=strlen(s3); if(s3[l-1]=='.'){ s3[l-1]='