题目地址:点击打开链接
题意:n个人,m个师徒关系,判断这m种师徒关系是否合法(如a既是b的徒弟又是b的师傅就不合法,师徒关系具有传递性)
思路:拓扑排序判环
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> using namespace std; const int maxn = 105; int map1[maxn][maxn]; int indegree[maxn]; int main() { int i; int n,m; int x,y; while(scanf("%d%d",&n,&m) && n) { queue<int> que; memset(map1,0,sizeof(map1)); memset(indegree,0,sizeof(indegree)); for(i=0; i<m; i++) { scanf("%d%d",&x,&y); if(!map1[x][y])//一种关系有可能输入几次,必须加一次判断 { map1[x][y] = 1; indegree[y]++; } } for(i=0; i<n; i++) { if(indegree[i] == 0) { que.push(i); } } int k,sum = 0; while(!que.empty()) { k = que.front(); que.pop(); sum++; for(i=0; i<n; i++) { if(map1[k][i]) { indegree[i]--; if(indegree[i] == 0) que.push(i); } } } if(sum == n) { printf("YES\n"); } else { printf("NO\n"); } } return 0; }
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> using namespace std; const int maxn = 105; int map1[maxn][maxn]; int indegree[maxn]; int main() { int i; int n,m; int x,y; while(scanf("%d%d",&n,&m) && n) { queue<int> que; memset(map1,0,sizeof(map1)); memset(indegree,0,sizeof(indegree)); for(i=0; i<m; i++) { scanf("%d%d",&x,&y); if(!map1[x][y])//一种关系有可能输入几次,必须加一次判断 { map1[x][y] = 1; indegree[y]++; } } for(i=0; i<n; i++) { if(indegree[i] == 0) { que.push(i); } } int k,sum = 0; while(!que.empty()) { k = que.front(); que.pop(); sum++; for(i=0; i<n; i++) { if(map1[k][i]) { indegree[i]--; } if(indegree[i] == 0)//这一个if没嵌到上一个if里面,导致出错 { que.push(i);//出错的原因如1->2->3,本来只有2能进队列,结果3也进去了 } } } if(sum == n) { printf("YES\n"); } else { printf("NO\n"); } } return 0; }