Legal or Not
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11170    Accepted Submission(s): 5230
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

C/C++:

 #include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0xffffff
using namespace std; const int my_max = ;
int n, m, a, b, my_indeg[my_max];
vector <int> my_G[my_max]; void topsort()
{
int my_cnt = ;
while ()
{
int temp = -;
for (int i = ; i < n; ++ i)
if (my_indeg[i] == )
{
temp = i, my_cnt ++, my_indeg[i] = -;
break;
} if (temp == -) break;
for (int i = ; i < my_G[temp].size(); ++ i)
-- my_indeg[my_G[temp][i]];
my_G[temp].clear();
} if (my_cnt == n)
printf("YES\n");
else
{
for (int i = ; i < n; ++ i)
my_G[i].clear();
printf("NO\n");
}
} int main()
{
while (~scanf("%d%d", &n, &m), n || m)
{
memset(my_indeg, , sizeof(my_indeg));
while (m --)
{
scanf("%d%d", &a, &b);
++ my_indeg[a];
my_G[b].push_back(a);
} topsort();
}
return ;
}

hdu 3342 Legal or Not (topsort)的更多相关文章

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

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

  2. 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 ...

  3. HDU 3342 Legal or Not(判断是否存在环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Othe ...

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

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

  5. HDU 3342 -- Legal or Not【裸拓扑排序 &amp;&amp;水题 &amp;&amp; 邻接表实现】

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

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

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

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

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

  8. HDU 3342 Legal or Not (最短路 拓扑排序?)

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

  9. HDU——3342 Legal or Not

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

随机推荐

  1. 深入了解一下HTTP缓存机制

    HTTP 缓存机制作为 web 性能优化的重要手段,是Web 开发知识体系库中的一个基础环节,但是对于很多学习者来说,仅仅只是知道浏览器会对请求的静态文件进行缓存,但是为什么被缓存,缓存是怎样生效的, ...

  2. 我要学并发-Java内存模型到底是什么

    内存模型 在计算机CPU,内存,IO三者之间速度差异,为了提高系统性能,对这三者速度进行平衡. CPU 增加了缓存,以均衡与内存的速度差异: 操作系统增加了进程.线程,以分时复用 CPU,进而均衡 C ...

  3. c++异常处理的方法

    c++异常处理 程序运行时常会碰到一些异常情况,例如:做除法的时候除数为 0:用户输入年龄时输入了一个负数:用 new 运算符动态分配空间时,空间不够导致无法分配:访问数组元素时,下标越界:打开文件读 ...

  4. linux+jenkins+github+python持续集成

    1.服务器上事先安装jenkins   参见:linux上war包方式安装Jenkins 2.新建一个自由风格的job,名字随意起 3.配置git(拉取github代码) 从github复制pytho ...

  5. jenkins中使用变量

    查看jenkins内置变量: 1.新建一个job: 2.构建-增加构建步骤-执行shell: 3.点击  可用的环境变量列表 即可查看 如WORKSPACE : 作为工作空间分配给构建目录的绝对路径 ...

  6. 微信小程序自定义弹窗(可通用)

    效果图 .wxml <cover-view class='mask' wx:if='{{isShow}}'> <cover-view class='modal'> <co ...

  7. MySQL基础篇(2)数据类型

    MySQL提供了多种数据类型,主要包括数值型.字符串类型.日期和时间类型. 1.数值类型 整数类型:TINYINT(1字节).SMALLINT(2字节).MEDIUMINT(3字节).INT(INTE ...

  8. HDFS基本命令与Hadoop MapReduce程序的执行

    一.HDFS基本命令 1.创建目录:-mkdir [jun@master ~]$ hadoop fs -mkdir /test [jun@master ~]$ hadoop fs -mkdir /te ...

  9. js滚动事件

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. swiper轮播

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...