题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342

Legal or Not

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

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
 
题目大意:有N个人,给出M组两个人之间的师徒关系,问是否存在悖论,即某个人的徒弟又是自己的师傅。
 
解题思路:用拓扑排序判断图中是否存在环即可
 
AC代码:
 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
queue <int > q;
int line[][];
int in[];
int main ()
{
int i,j,m,n,num,u,v;
while (scanf("%d%d",&n,&m)!=EOF)
{
if (n == )
break;
memset(line,,sizeof(line));
memset(in,,sizeof(in)); for (i = ; i < m; i ++)
{
scanf("%d%d",&u,&v);
if (line[u][v]==) //判断重边
{
line[u][v] ++;
in[v] ++;
}
}
for (i = ; i < n; i ++)
if (in[i] == ) q.push(i); //入度为0的存入队列
num = ;
while (!q.empty())
{
int k = q.front();
q.pop();
num ++;
for (i = ; i < n; i ++)
{
if (line[k][i] > ) //删掉与该入度为0的节点相连的边
{
in[i] --;
if (in[i] == )
q.push(i);
}
}
}
if (num == n) //判断是否存在环
printf("YES\n");
else
printf("NO\n");
}
return ;
}

HDU 3342 Legal or Not(判断是否存在环)的更多相关文章

  1. HDU 3342 Legal or Not (图是否有环)【拓扑排序】

    <题目链接> 题目大意: 给你 0~n-1 这n个点,然后给出m个关系 ,u,v代表u->v的单向边,问你这m个关系中是否产生冲突. 解题分析: 不难发现,题目就是叫我们判断图中是否 ...

  2. HDU 3342 Legal or Not(有向图判环 拓扑排序)

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  3. HDU 3342 Legal or Not (图是否有环)

    题意: 给出n个人的师徒关系,如有 a是b的师傅,b是c的师傅,c是a的师傅,这样则不合法,输出NO,否则输出YES. 思路: 每段关系可以看成一条有向边,从师傅指向徒弟,那么徒弟的徒子徒孙都不可能再 ...

  4. HDU.3342 Legal or Not (拓扑排序 TopSort)

    HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...

  6. hdu 3342 Legal or Not

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Description ACM-DIY is a large QQ g ...

  7. HDU 3342 Legal or Not(判断环)

    Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is so h ...

  8. HDU——3342 Legal or Not

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. hdu 3342 Legal or Not(拓扑排序)

    Legal or Not Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

随机推荐

  1. linux中用户的主目录~

    -在Linux中代表用户主目录 对一般用户,-表示/home/(用户名) 对于root用户,-表示/root 如果要查看-的真实面貌,可以先进入-目录,然后用pwd -P命令查看-的绝对路径 cd ~ ...

  2. Android中View的基础知识

    View的界限 View就是我们看到的界面,有四个界限范围分别是, Top/Left, Bottom/Right,坐标系从左上到右下.这四个值可以通过任何View的子类调用getTop()/get.. ...

  3. js动画之缓冲运动

    缓冲运动就是运动的速度与时间或者距离有关联,不是一般的匀速运动 <!DOCTYPE html> <html lang="en"> <head> ...

  4. 有关C# struct的一个误区

    参考:http://blog.csdn.net/onlyou930/article/details/5568319 下面写一个新的例子: using System; using System.Coll ...

  5. 关于 DWZ 弹出框

    1.弹出框 <a name="***" class="***" href="${** }/***.do?action=***&属性=${ ...

  6. 实验二 简易版C语言文法

    <程序>::=begin<语句串>end <语句串>::=<语句>{;<语句>} <语句>::=<赋值语句> < ...

  7. css3动画之背景颜色的自动切换

    因为不同浏览器内核的不同所以会产生浏览器兼容性问题 <!DOCTYPE html> <html> <head> <meta charset='utf-8'/& ...

  8. 2014年6月份第4周51Aspx源码发布详情

    通用医院会员管理系统源码  2014-6-23 [VS2010]功能介绍:本系统共包括以下模块:会员开卡管理.会员充值管理.会员消费管理.会员病例管理.客户预约管理.系统信息管理(门诊管理.卡类型管理 ...

  9. C#和Java在重写上的区别

    C# class A { public string Get1() { return "A1"; } public virtual string Get2() { return & ...

  10. js 表单验证方法二

    function ckReight () { var pass = true; var new = $("#new"); if( new.find('input[name=name ...