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. PHP array_unshift

    1.函数的作用:在数组的开头插入一个或者多个元素 2.函数的参数: @params  array  &$array @params  mixed $value1 @params  mixed ...

  2. 图像处理 - ImageMagick 简单介绍与案例

    在客户端我们可以用 PhotoShop 等 GUI 工具处理静态图片或者动态 GIF 图片,不过在服务器端对于 WEB 应用程序要处理图片格式转换,缩放裁剪,翻转扭曲,PDF解析等操作, GUI 软件 ...

  3. Hystrix dashboard - Unable to connect to Command Metric Stream.

    在使用boot 2.0.*以上版本 + cloud Finchley.RELEASE 查看仪表盘的时候会报错 Unable to connect to Command Metric Stream &l ...

  4. justjavac(迷渡)知乎live--<<前端工程师的入门与进阶>>听讲总结

    知乎听讲总结 知乎live----jjc<前端工程师的入门进阶> git地址 内容 前端的基础知识,计算机专业基础知识感觉还行.前端后台都有做过,现在觉得自己要深入.但是只看框架源码和自己 ...

  5. 前后端hosts配置访问问题解决思路

    问题背景:前后端分离情况下后端开发测试需要配置hosts,有此问题的人员有RD,QA,PM,User 测试环境由于用户使用这种配置导致无法使用线上系统发起单据影响用户使用,同时让用户误以为系统出问题而 ...

  6. Andriod Studio设置类默认签名模板

  7. sql 经典查询50题 思路(一)

    因为需要提高一下sql的查询能力,当然最快的方式就是做一些实际的题目了.选择了这个sql的50题,这次大概做了前10题左右,把思路放上来,也是一个总结. 具体题目见: https://zhuanlan ...

  8. docker-compose下的java应用启动顺序两部曲之二:实战

    上篇回顾 本文是<docker-compose下的java应用启动顺序两部曲>的终篇,在上一篇<docker-compose下的java应用启动顺序两部曲之一:问题分析>中,我 ...

  9. Vue学习系列(四)——理解生命周期和钩子

    前言 在上一篇中,我们对平时进行vue开发中遇到的常用指令进行归类说明讲解,大概已经学会了怎么去实现数据绑定,以及实现动态的实现数据展示功能,运用指令,可以更好更快的进行开发.而在这一篇中,我们将通过 ...

  10. 中文预训练模型ERNIE2.0模型下载及安装

    2019年7月,百度ERNIE再升级,发布持续学习的语义理解框架ERNIE 2.0,及基于此框架的ERNIE 2.0预训练模型, 它利用百度海量数据和飞桨(PaddlePaddle)多机多卡高效训练优 ...