拓扑排序 --- 判断是否有回路

Legal or Not


Time
Limit: 2000/1000 MS (Java/Others)    Memory Limit:
32768/32768 K (Java/Others)
Total Submission(s):
3823    Accepted Submission(s):
1738

Problem Description

ACM-DIY is a large QQ group where many excellent
acmers get together. It is so harmonious that just like a big family. Every
day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line
to exchange their ideas. When someone has questions, many warm-hearted cows
like Lost will come to help. Then the one being helped will call Lost "master",
and Lost will have a nice "prentice". By and by, there are many pairs of
"master and prentice". But then problem occurs: there are too many masters and
too many prentices, how can we know whether it is legal or not?

We all
know a master can have many prentices and a prentice may have a lot of masters
too, it‘s legal. Nevertheless,some cows are not so honest, they hold illegal
relationship. Take HH and 3xian for instant, HH is 3xian‘s master and, at the
same time, 3xian is HH‘s master,which is quite illegal! To avoid this,please
help us to judge whether their relationship is legal or not.

Please
note that the "master and prentice" relation is transitive. It means that if A
is B‘s master ans B is C‘s master, then A is C‘s master.

Input

The input consists of several test cases. For each
case, the first line contains two integers, N (members to be tested) and M
(relationships to be tested)(2 <= N, M <= 100). Then M lines follow, each
contains a pair of (x, y) which means x is y‘s master and y is x‘s prentice. The
input is terminated by N = 0.
TO MAKE IT SIMPLE, we give every one a number
(0, 1, 2,..., N-1). We use their numbers instead of their names.

Output

For each test case, print in one line the judgement
of the messy relationship.
If it is legal, output "YES", otherwise
"NO".

Sample Input

3 2

0 1

1 2

2 2

0 1

1 0

0 0

Sample Output

YES

NO

【题目来源】

HDOJ
Monthly Contest – 2010.03.06

【题目大意】

ACM-DIY这个QQ群里有很多神牛,也有一些像我这样的渣渣,渣渣们经常在问问题,久而久之,就形成了师徒关系,但是有些师徒关系是不合法的,比如说:A是B的师傅,而B又是A的师傅,总之形成了环就是不合法的,现在给你一些师徒关系,让你判断是否合法。

【题目分析】

其实就是给你一个图,让你判断是否会形成回路,如果会就是不合法的,否则合法。

简单的拓扑排序


#include<iostream>
#include<cstring>
#define MAX 110
using namespace std;
int n,m,a,b,i,j,k,x;
bool Map[MAX][MAX];
int indegree[MAX];

void topsort()
{
for(k=1,i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(!indegree[j])
{
k++;
indegree[j]--;
for(x=1;x<=n;x++)
if(Map[j][x])
indegree[x]--;
break;
}
if(j>n)
return;
}
}
}
int main()
{
while(cin>>n>>m,n)
{
memset(Map,0,sizeof(Map));
memset(indegree,0,sizeof(indegree));
for(i=1;i<=m;i++)
{
cin>>a>>b;
a++;
b++;
if(!Map[a][b])
{
Map[a][b]=1;
indegree[b]++;
}
}
topsort();
if(k-1==n)
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}

拓扑排序 --- 判断是否有回路,布布扣,bubuko.com

时间: 2024-08-24 17:20:50

拓扑排序 --- 判断是否有回路的相关文章

hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】

Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3566    Accepted Submission(s): 1395 Problem Description Recently, scientists find that there is love between any of two people. For

拓扑排序 判断给定图是否存在合法拓扑序列 自家oj1393

1 //拓扑排序判断是否有环 2 #include<cstdio> 3 #include<algorithm> 4 #include<string.h> 5 #include<math.h> 6 #include<queue> 7 using namespace std; 8 typedef long long ll; 9 const int maxn=1e2+10; 10 int G[maxn][maxn]; 11 int in[maxn];

拓扑排序判断有向图是否有回路

1 #include <iostream> 2 #include <queue> 3 #include <string> 4 using namespace std; 5 6 //表结点 7 typedef struct ArcNode{ 8 int adjvex;//该弧所指向的顶点的位置 9 ArcNode *nextarc; 10 }ArcNode; 11 12 //头结点 13 typedef struct VNode{ 14 string data;//顶点信

Lightoj 1003 - Drunk(拓扑排序判断是否有环 Map离散化)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1003 题意是有m个关系格式是a b:表示想要和b必须喝a,问一个人是否喝醉就看一个人是否能把所有种类的饮料喝完,能输出Yes,不能输出No: 其实就是有向图判断是否存在环,可以用拓扑排序来求 拓扑排序:每次找到一个入度为0的点加入队列,删除与之相连的边,循环操作,得到的序列就是拓扑序(前面的必须出现在后面的前面): #include<stdio.h> #include<str

HDU 3342 Legal or Not(拓扑排序判断成环)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即可. 代码: 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int N=1e2+5; 6 7 int n,m; 8 int vis[N];

UVA-1572 Self-Assembly(拓扑排序判断有向环)

题目: 给出几种正方形,每种正方形有无穷多个.在连接的时候正方形可以旋转.翻转. 正方形的每条边上都有一个大写英文字母加'+'或'-'.00,当字母相同符号不同时,这两条边可以相连接,00不能和任何边相连. 判断给出的正方形如果能无限连接下去就输出unbounded.不能就输出bounded. 思路: 这个题读完之后,一点思路都没有,看完紫书上的分析知道是用拓扑排序来判断有向环,但具体的构造还是不会-- 1.将每个正方形看作一条边,在正方形每两条边上的字母(不包括00)之间连一条有向边构成一个有

POJ 1094 Sorting It All Out(拓扑排序&#183;判断+实现)

题意  由一些不同元素组成的升序序列是可以用若干个小于号将所有的元素按从小到大的顺序 排列起来的序列.例如,排序后的序列为 A, B, C, D,这意味着 A < B.B < C和C < D.在本题中, 给定一组形如 A < B的关系式,你的任务是判定是否存在一个有序序列. 输出到哪一项可以确定顺序或者在这一项最先出现冲突,若所有的小于关系都处理完了都不能确定顺序也没有出现冲突,就输出不能确定 每来一个小于关系就进行一次拓扑排序  直到出现冲突(也就是出现了环)或者已经能确定顺序

【图论】拓扑排序应用

拓扑排序虽是一种排序,但是它跟平时所接触的sort或者qsort不同,排序的意义不同.拓扑排序针对有向无回路图(DAG)而言的,不应用与存在回路的有向图. [图论]广度优先搜索和深度优先搜索 有说到了BFS和DFS,拓扑排序是DFS的一个应用. 有向无回路图能说明事件的发生的先后的顺序.比如穿衣服,士兵排队等.一个具体的例子,有N个物体,下面给出物体的重量比较,比如(a,b)表示a比b重等等,问已给出的条件是否会矛盾?其实就是判断用所给条件所组织的一个图中是否会存在环? 在DFS中加入时间戳,完

HDU 4324 (拓扑排序) Triangle LOVE

因为题目说了,两个人之间总有一个人喜欢另一个人,而且不会有两个人互相喜欢.所以只要所给的图中有一个环,那么一定存在一个三元环. 所以用拓扑排序判断一下图中是否有环就行了. 1 #include <cstdio> 2 #include <cstring> 3 4 const int maxn = 2000 + 10; 5 char G[maxn][maxn]; 6 int c[maxn]; 7 int n; 8 9 bool dfs(int u) 10 { 11 c[u] = -1;