1 //只需要判断入度为零的点是不是只有1个就可以了 2 #include<stdio.h> 3 #include<string.h> 4 #include<string> 5 #include<algorithm> 6 #include<map> 7 using namespace std; 8 #define maxn 1005 9 int indegree[maxn]; 10 map<string,int>re; 11 void init() 12 { 13 re.clear(); 14 memset(indegree,0,sizeof(indegree)); 15 } 16 void topsort(int num) 17 { 18 int count,i; 19 count=0; 20 for(i=1;i<=num;i++) 21 if(indegree[i]==0) 22 count++; 23 if(count==1) 24 printf("Yes\n"); 25 else 26 printf("No\n"); 27 } 28 int main() 29 { 30 char a[20],b[20]; 31 int n,i,j,people; 32 while(scanf("%d",&n)!=EOF&&n) 33 { 34 init(); 35 people=1; 36 for(i=1;i<=n;i++) 37 { 38 scanf("%s%s",a,b); 39 if(re[a]==0) 40 re[a]=people++; 41 if(re[b]==0) 42 re[b]=people++; 43 indegree[re[b]]++; 44 } 45 topsort(people-1); 46 } 47 return 0; 48 }