题目链接: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. Coding源码学习第四部分(Masonry介绍与使用(二))

    接上篇,本篇继续对Masonry 进行学习,接上篇示例: (6)Masonry 布局实现iOS 计算器 - (void)exp4 { WS(weakSelf); // 申明区域,displayView ...

  2. java 通过反射获取调用类方法及属性

    首先说下反射是什么?反射是Sun公司推出的一组API,此组API位于Java.lang.reflect中 反射的作用是编写工具(例如eclipse),编写框架,当然对于一般的程序,我们不可能用反射来做 ...

  3. 管理分支:git branch

    新建分支的意义: 创建一个单独的工作分支,避免对主分支master造成太多的干扰,也方便与他们交流协作. 进行高风险的工作时,创建一个实验性的分支,扔掉一个烂摊子总比收拾一个烂摊子好得多. 合并别人工 ...

  4. VMware下利用ubuntu13.04建立嵌入式开发环境之五

    tftp和nfs服务器配置 一.tftp服务器配置 1.安装软件包 1.1安装服务 apt-get install tftpd-hpa 1.2安装客户端 apt-get install tftp-hp ...

  5. scala学习----柯里化

    1.鸭子类型,走起来像鸭子,叫起来像鸭子,就是鸭子.函数中使用{ def close(): Unit }作为参数类型,因此任何含有此函数的类都可以作为参数传递.好处是不必使用继承特性. def wit ...

  6. hibernate中validate的使用(转)

    原文链接:http://blog.csdn.net/xing_sky/article/details/8484551 首先是要加入下面两个包 hibernate-validator-4.1.0.Fin ...

  7. iOS开发一个用户登录注册模块需要解决的坑

    最近和另外一位同事负责公司登录和用户中心模块的开发工作,开发周期计划两周,减去和产品和接口的协调时间,再减去由于原型图和接口的问题,导致强迫症纠结症状高发,情绪不稳定耗费的时间,能在两周基本完成也算是 ...

  8. matlab初学之plot颜色和线型

    文章出处: http://jiangshuxia.9.blog.163.com/blog/static/3487586020116711375339/ 字母        颜色          标点 ...

  9. ajax 中boolean值技巧

    // 利用判断 数据重复 function checkId () { var flag = true; $.ajax({ url: "", type: "post&quo ...

  10. 自定义jsp标签

    1.类: package ===============================; import javax.servlet.jsp.JspTagException; import javax ...